Creating timers for specific players

I am trying to make a health station. When you walk up and press E, it will give you health in exchange for money. When you activate it, a timer begins for 300 seconds. You can not use the health again until the timer is up. Everything works flawlessly, except for the fact that the timers are global… I need to find a way to make them playerbased. Any ideas?

Code:
[LUA]//Anarchy
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)
AddCSLuaFile(“settings.lua”)
include(“shared.lua”)
include(“settings.lua”)

function ENT:Initialize()
self:SetModel(“models/props_combine/health_charger001.mdl”)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
local phys = self:GetPhysicsObject()
if phys:IsValid() then phys:Wake() end
end

function itstime()
timer.Destroy( “interval” )
end

function ENT:Use( activator, caller )

/*if activator != self.dt.owning_ent then
	DarkRP.notify( activator, 1, 4, "You're not the owner of this Health Station." ) 
	return
end*/

local ply = activator
local health = ply:Health()
local timeleft = timer.TimeLeft( "interval" )

if ( timer.Exists( "interval" ) ) then DarkRP.notify(ply, 1, 5, "You must wait "..timeleft.." seconds before buying health again!") return end
if not ( timer.Exists( "interval" ) ) then
	if health < 100 then
		if ply:canAfford(HHS.Price) then
			ply:addMoney(-HHS.Price)
			ply:SetHealth( 100 )
			DarkRP.notify(ply, 4, 3, "You have bought health for $"..HHS.Price..".")
			timer.Create( "interval", HHS.Interval, 1, itstime )
		end
		if not ply:canAfford(HHS.Price) then
			DarkRP.notify(ply, 1, 3, "You can't afford to buy health!")
		end
	end
end

end

function ENT:Think()
end
[/LUA]

This is not expert advice or anything but you can create unique timers by using the player’s SteamID.

Instead of just timer.Create( “interval” ) you could do timer.Create( “interval” … ply:SteamID() ) as an example. That way there can be a timer tied to each individual player.

Similarily, you would check if the timer exists with something like timer.Exists( “interval” … ply:SteamID() ).

Hadn’t thought of doing that! I’ll give it a shot.

[editline]27th August 2014[/editline]

Code worked well. I’ve run into another problem though. Now it no longer gives me a fail message when the timer is not up. Here is that line of code.

[LUA] if ( timer.Exists( “interval”…ply:SteamID() ) ) then DarkRP.notify(ply:SteamID(), 1, 5, “You must wait “…timeleft…” seconds before buying health again!”) return end[/LUA]

Can you post your code where you create the timer?

why not SteamID64s?

Instead of using a Timer, why not just do something else?

https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/simple_cooldown.lua.html

So, essentially with your code ( I changed a few things because some things were not consistent, then I also removed some redundant checks [ no point using 2 ifs when 1 if / else will do the same thing, prevent logic from being checked that doesn’t need it ]; for what I added I used my _var names which you may change, of course :slight_smile: – No need to run a function at the end of the timer to destroy a timer, because when the timer executes it should be removed if 1 rep is set ):


//Anarchy
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "settings.lua" )
include( "shared.lua" )
include( "settings.lua" )

function ENT:Initialize()
	self:SetModel( "models/props_combine/health_charger001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetUseType( SIMPLE_USE )
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then phys:Wake() end
end

function ENT:Use( activator, caller )

	/*if activator != self.dt.owning_ent then
		DarkRP.notify( activator, 1, 4, "You're not the owner of this Health Station." )
		return
	end*/

	local ply = activator
	local health = ply:Health()

	local _bCooldown, _timeleft = ply:HasCooldown( "health_charger" );
	if ( !_bCooldown ) then
		if health < 100 then
			if ply:canAfford( HHS.Price ) then
				ply:addMoney( -HHS.Price )
				ply:SetHealth( 100 )
				DarkRP.notify( ply, 4, 3, "You have bought health for $"..HHS.Price.."." )
				ply:SetCooldown( "health_charger", HHS.Interval );
			else
				DarkRP.notify( ply, 1, 3, "You can't afford to buy health!" )
			end
		end
	else
		DarkRP.notify( ply, 1, 5, "You must wait " .. _timeleft .. " seconds before buying health again!" )
	end

end

function ENT:Think()
end

EDIT: Changed a few more things to simplify how many tabs were used:


//Anarchy
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "settings.lua" )
include( "shared.lua" )
include( "settings.lua" )

function ENT:Initialize()
	self:SetModel( "models/props_combine/health_charger001.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetUseType( SIMPLE_USE )
	local phys = self:GetPhysicsObject()
	if phys:IsValid() then phys:Wake() end
end

function ENT:Use( activator, caller )

	/*if activator != self.dt.owning_ent then
		DarkRP.notify( activator, 1, 4, "You're not the owner of this Health Station." )
		return
	end*/

	// No point to continue if the activator isn't a player...
	local ply = activator
	if ( !IsValid( ply ) || !ply:IsPlayer( ) ) then return; end

	// No point continuing if health is max
	local health = ply:Health( )
	if ( health == ply:GetMaxHealth( ) ) then return; end

	local _bCooldown, _timeleft = ply:HasCooldown( "health_charger" );
	if ( !_bCooldown ) then
		if ply:canAfford( HHS.Price ) then
			ply:addMoney( -HHS.Price )
			ply:SetHealth( 100 )
			DarkRP.notify( ply, 4, 3, "You have bought health for $"..HHS.Price.."." )
			ply:SetCooldown( "health_charger", HHS.Interval );
		else
			DarkRP.notify( ply, 1, 3, "You can't afford to buy health!" )
		end
	else
		DarkRP.notify( ply, 1, 5, "You must wait " .. _timeleft .. " seconds before buying health again!" )
	end

end

function ENT:Think()
end

Edit: I made a small typo, it is fixed now:

https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/simple_cooldown.lua.html

Hopefully these tutorials and information helps. As always, to view the Lua from any of my tutorials to enable copy/pasting ( HTML doesn’t copy/paste well in terms of HTML / CSS Highlighted Lua ), remove .html from the url.

Awesome, thank you for all the information and help everyone. All of you were extremely helpful! And thank you AceCool for the code. Never realized my work was so redundant lol.

If you want help, feel free to add me on Steam. I tutor a lot of others and have written around 400 tutorials so far with more coming any time I answer new questions such as this one.

I probably should’ve written the cooldown helper-functions a long time ago for how often “cooldowns” are used, so thank you for planting the seed.

If the thread questions have been solved, please mark the topic as solved by clicking “Mark As Solved” in the upper left-hand corner of the page.