Stack Overflow, no idea why it's doing it

This is running every Think:



local maxdist = 200
for _,v in ipairs(ents.FindByClass("prop_physics")) do
	if ValidEntity(v) and v.IsThrownRock and v:GetPos():Distance(ply:GetPos()) <= maxdist then
		v:GetPhysicsObject():ApplyForceCenter((v:GetPos() - ply:GetPos())*2000)
	end
end


However, it breaks on the first run with the error: Stack Overflow.
I’ve been told this is when it loops through and overflows the stack, but there aren’t any entities near me, aside from the world.

Any ideas?

Try and add a validation to the Physics Object -> if v:GetPhysicsObject():IsValid() then … (Although I’m not sure it will solve your problem, but it’s recommended )

Well, there is no reason it should break from the code you posted. Where does ply come from? Can you post the full code, or something?



ITEM.Think = function( ply )
		local maxdist = 200
		for _,v in ipairs(ents.FindByClass("prop_physics")) do
			if ValidEntity(v) and v.IsThrownRock and v:GetPos():Distance(ply:GetPos()) <= maxdist then
				v:GetPhysicsObject():ApplyForceCenter((v:GetPos() - ply:GetPos())*2000)
			end
		end
	end


ITEM:Think( ply ) is run every think on the item, using this code:



hook.Add("Think","ItemThink",function()
	for _,v in ipairs(player.GetAll()) do
		local item = v:GetItemTable()
		if item and item.Think then
			item:Think(v)
		end
	end
end)


That looks wrong to me, ITEM.Think is a function that takes only a player as an argument, and yet you’re calling item:Think(v), which actually implies item.Think(item, v). Not sure that’s why you are having issues, but try item.Think(v)

Could be right, that was pretty dumb of me D:

Perfect, thanks :smiley: