Different way's to stop explpoits with .net libary

So I have some code which will grow a plant which is like this:


net.Receive( "start_pop_grow", function ( len, ply )
	ent = net.ReadEntity()
	if ent.cangrow == true then 
	ent.cangrow = false
	ent.cancollect = false
	print ( ent.cangrow )
	growtime = 2
		if ent:GetPos():Distance(ply:GetPos()) > 100 then return end
		ent:SetModel("models/pot/pot_06.mdl")
			if !IsValid(ent) then return end
			timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
				if !IsValid(ent) then return end
				ent:SetModel("models/pot/pot_07.mdl")
				timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
					if !IsValid(ent) then return end
					ent:SetModel("models/pot/pot_08.mdl")	
					timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
						if !IsValid(ent) then return end
						ent:SetModel("models/pot/pot_09.mdl")
						timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
							if !IsValid(ent) then return end
							ent:SetModel("models/pot/pot_10.mdl")
							ent.cangrow = true
							ent.cancollect = true
							print (ent.cangrow)
						end)
					end)
				end)
			end)
		end
	end
end)

This is in the init.lua and in the cl_init.lua I have a derma button which will set off this function above.

However, I believe it is possible to call this function from across the map by sending “start_pop_grow” to the server? In the script I have put:


if ent:GetPos():Distance(ply:GetPos()) > 100 then return end

This does not work however? Why is this, and is there other ways so people cannot call this from across the map? Or is this the only way?

Thanks in advance!

(Note, I always get the < and > mixed up and I have tried both)

Check at the top of the recieve instead.

I will try and get back to you - thanks for the reply

[editline]13th August 2015[/editline]

It did not work I am afraid. Any other ways? Or ideas?

[editline]13th August 2015[/editline]

I managed to do it in the end" It was just something really simple… hahaha. It was just this, in-case anyone else has this problem:


net.Receive( "start_pop_grow", function ( len, ply )
	ent = net.ReadEntity()
	if ent:GetPos():Distance(ply:GetPos()) < 70 then
	if ent.cangrow == true then 
	ent.cangrow = false
	ent.cancollect = false
	print ( ent.cangrow )
	growtime = 2
	ent:SetModel("models/pot/pot_06.mdl")
	if !IsValid(ent) then return end
	timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
		if !IsValid(ent) then return end
		ent:SetModel("models/pot/pot_07.mdl")
		timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
			if !IsValid(ent) then return end
			ent:SetModel("models/pot/pot_08.mdl")	
			timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
				if !IsValid(ent) then return end
				ent:SetModel("models/pot/pot_09.mdl")
				timer.Create( "timer"..ent:EntIndex(), growtime, 1, function()
					if !IsValid(ent) then return end
					ent:SetModel("models/pot/pot_10.mdl")
					ent.cangrow = true
					ent.cancollect = true
					print (ent.cangrow)
				end)
			end)
		end)
	end)
	end
	end
end)

However, one problem persists. If they spawn in two of the entities, when they grow on, it will grow, but when they start to grow another it will stop the other one from growing and the new one spawned will jump to the timer that the other one was at?! I understand that something with EntIndex could be used to identify each one as a different one, but how would I do this?

Hold on a minute

[editline]13th August 2015[/editline]

Something doesn’t seem quite right

[editline]13th August 2015[/editline]

I can’t seem to be able to put my finger on it

[editline]13th August 2015[/editline]

Why are you nesting 10000 timers?

Oh right, have you got another way of doing it?

Recursive functions. When you code you want to try to eliminate as much boilerplate, or repeated, code as possible just to make life easy. Taking advantage of function’s ability to call themselves allows you to accomplish exactly what you wrote but with less code



local count = 1
local function timerFunction()
	count = count + 1
	
	if count == 5 then
		
		return
	end
	
	timer.Create( "timer"..ent:EntIndex(), growtime, 1, timerFunction )
end