Troubles getting for k,v to work

I’m trying to find all the physic props in a sphere and weld them to each other , but I’m running into certain difficulties… such as it basically welding to itself. If anybody can see something wrong with this please give me a suggestion as to how to fix it.

[lua]
local shit = ents.FindInSphere(entpos,1000)
–findClipToClass(“prop_physics”)
PrintTable(shit)

--constraint.Weld(origin_ents,origin_ents,0,0,true)

for k,v in pairs(shit) do

– print( k )
if (v:GetClass() == “prop_physics”) then

print( "the prop is :" .. tostring(v) .. "muffuka")    
    
    
    
        
    

constraint.Weld(v[k],v[k + 1],0,0,true)
print("welded" .. tostring(v[k]) .. "to " .. tostring(v[k + 1]))
end
end

[/lua]

Oh my god, learn basic lua before attempting things.

tostring(v) - You cannot print an entity
.Weld(v[k] - I dont even know

:frog:

[lua]print(Entitiy(1))[/lua]

Give it a try Science

In a loop, when looping with entities, K will be the index the entity got in table (“shit” in your case), V will be the actual entity. Doing V[k] will result in madness. You’d want todo something like
[lua]constraint.Weld(v,shit[k + 1],0,0,true)[/lua] instead.

Thank you very much Donkie , and science , I already know far more than basic lua , I stopped scripting for gmod for more than a year and thus very rusty , but i’ve already scripted a full RPG from scratch with inventory and everything when it was still considered “special” to have an inventory…

And FYI , the tostring is exactly there for that reason , because you can’t print an entity.

you can print an entity

You can’t concat an entity to a string.

Why are you replying to him, he got pbanned anyway?

I don’t see why people rated me dumb for my explanation , it’s meant as a way to debug , not actually print the entity.

They rated you dumb because you CAN use tostring with an entity, it’ll return a combination of it’s id and class… It was even said in a post above yours.

That’s exactly what I said in my explanation , but without the tostring you can’t print it…

Again, you fail to read the posts above yours. You CAN print an entity, you might not be able to concatenate it, but you can still print an entity.

When I tried it gave me errors. But anyways , let’s agree to disagree.

print() will print and entity by it’s self, but you can not concatenate an entity without tostring(), same with vectors, etc. I do it all the time to check what entity’s are getting passed to hooks. So how about everyone stop arguing about it.



] rcon lua_run print(ents.GetAll()[1])
Entity [0][worldspawn]
] rcon lua_run print("This is the world entity" .. ents.GetAll()[1])
[@lua_run:1] bad argument #2 to ? (string expected, got Entity)


So Zencro, as far as what you are trying the best way I can think of to cut down on the number of welds is to pick one entity in the radius and weld everything else to that one, like this.

[lua]

local entPos = Vector(0,0,0) – Whatever point you are looking for entities
local entsInSphere = ents.FindInSphere(entPos, 1000) – Use descriptive names even in testing, it helps
local rndEntNum = math.random(1,table.Count(entsInSphere)) – Pick a random ent to weld to
local weldEnt = entsInSphere[rndEntNum] – Now get the random ent
table.remove(entsInSphere, rndEntNum) – Take it out of the table so we don’t weld it to it self.

for k, ent in pairs(entsInSphere) do – Loop through the rest of the table you can use k,v but again descriptive is better.
constrain.Weld(weldEnt, ent, 0, 0, 0, true) – Weld every ent in the table to the one entity we picked
end

[/lua]

I wrote this in the browser window, but it should work. I hope the comments help.

Oh no , the script was working long ago , I’m just wondering why people are rating as dumb when it’s in fact truth that I say.

By the way thank you very much for the will to help , but the welding to one prop would be problematic with the script I’m making since the props get destroyed , and if the welded prop gets destroyed the whole things gets unwelded and crumbles.

Ok, well I didn’t see that it was resolved.