Timer in a Think hook

Simple question:

How do I make a timer inside a Think hook, without getting the timer repeated by the Think hook?

It’s for a GetInSphere script.

Why do you need to make a timer in a think hook?

The Think hook needs to check if the player is inside the sphere, but the timer needs to do the action.

Check if the timer exists with **[Timer.IsTimer

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Timer.IsTimer)**, then if it does, don’t create a new one?

I tried this, but doesn’t work. What did I do wrong?

[lua]
for k, v in pairs(ents.FindInSphere(Vector(-6461.760254, -6707.573730, 128.031250), 80)) do
if(v:IsPlayer() and v:Alive() and v:InVehicle() and v:GetVehicle():GetTable()._Owner == v) then
if(timer.IsTimer(“SkinSwitcher”)) then
timer.Destroy(“SkinSwitcher”)
end;
else
timer.Create( “SkinSwitcher”, 2, 0, function()
cider.player.notify(v, “Just a message to test it…”, 0)
end)
end;
end;
[/lua]

That’s because your checking if the timer exists, then if it doesn’t your creating one, then on the next tick it’s destroying the timer you just created.

Try:

[lua]
for k, v in pairs(ents.FindInSphere(Vector(-6461.760254, -6707.573730, 128.031250), 80)) do
if(v:IsPlayer() and v:Alive() and v:InVehicle() and v:GetVehicle():GetTable()._Owner == v) and not timer.IsTimer( “SkinSwitcher” ) then

    timer.Create( "SkinSwitcher", 2, 0, function()
        cider.player.notify(v, "Just a message to test it..", 0)
    end)

end;

end;
[/lua]

I don’t understand what you’re trying to do

creating a timer over and over again with for instance a delay of 1 second will not execute the timer until you leave the sphere a second later

I’m assuming you want to change the skin on enter and on leave?

[lua]local point = Vector(-6461.760254, -6707.573730, 128.031250)

timer.Create(“SkinSwitcher”, 0.3, 0, function()
for key, ply in pairs(player.GetAll()) do

	-- I'm pretty sure vehicle:GetTable()._Owner is the same as just indexing the vehicle
	if ply:Alive() and ply:GetVehicle()._Owner = ply then
		-- this will always be valid, because we checked if the variable _Owner is the player
		-- and NULL._Owner == ply is false because _Owner in NULL will always be nil
		local vehicle = ply:GetVehicle()
		
		if ply:GetPos():Distance(point) < 80 then
		
			-- only call it once
			if not ply._vehicle_skin_set and then
				vehicle:SetSkin(2)
				ply._vehicle_skin_set = true
			end
			
		else
		
			if not ply._vehicle_skin_set and then
				vehicle:SetSkin(2)
				ply._vehicle_skin_set = true
			end
			
		end
	end
end

end)[/lua]