Gmod Darkrp Coder

To all the lua coders out there, I need help creating a command that will allow the mayor to trigger an entity and charge the mayor some money. What I hope to accomplish is have the mayor be able to ‘purchase’ electricity (since in North Korea they don’t have much electricity because they’re poor) and cost him like 2.5 K or something like that.

I already have all the lights hooked up to one entity (named ‘lightcont’) and all that needs to happen is have him be able to trigger it and charge the mayor some money.

The way to control this entity in single player would be typing ‘ent_fire lightcont trigger’ in console with sv_cheats 1

Is it a SENT? If so, then self:SetUseType( SIMPLE_USE ) in Initialize() then in ENT:Use() do something along the lines of this:
[lua]function ENT:Use( ply )
if not ply:Team() == TEAM_MAYOR then return end
if not ply:canAfford( your_price_here ) then DarkRP.notify( ply, “You cannot afford electricity” ) return end

ply:addMoney( -your_price_here )
– self:Fire( whatever ), not sure about triggers
end[/lua]
Otherwise I think you’d be forced to use PlayerUse to control the buying, something like this
[lua]hook.Add( “PlayerUse”, “Electricity”, function( ply, ent )
if not ent:GetClass() == “lightcont” or not ply:Team() == TEAM_MAYOR or GAMEMODE.Electricity then return end
if not ply:canAfford( your_price_here ) then DarkRP.notify( ply, “You cannot afford electricity” ) return end

ply:addMoney( -your_price_here )
-- self:Fire( whatever ), not sure about triggers
GAMEMODE.Electricity = true

end )[/lua]

No I’m pretty sure it’s not a sent, it’s incorporated into the map itself, the entity itself is a logic_relay, but it’s name is lightcont. Also I would like the power to only stay on for like 20 minutes or so before the power turns back off.

Give this a shot, put it in a server side file (eg. lua/autorun/server/<myfile>.lua)
[lua]hook.Add( “Initialize”, “GetElectricityEnt”, function()

timer.Simple( 1, function()

	local ent = ents.FindByClass( "lightcont" )
	if type( ent ) == "table" then ent = ent[1] end

	GAMEMODE.ElectricityEnt = ent

end

end )

hook.Add( “PlayerUse”, “BuyElectricity”, function( ply, ent )
if not ent == GAMEMODE.ElectricityEnt or not ply:Team() == TEAM_MAYOR or GAMEMODE.ElectricityOn then return end
if not ply:canAfford( your_price_here ) then DarkRP.notify( ply, “You cannot afford electricity” ) return end

ply:addMoney( -your_price_here )
ent:Fire( "trigger" )
GAMEMODE.ElectricityOn = true

timer.Simple( 1200, function()

	ent:Fire( "trigger" )
	GAMEMODE.ElectricityOn = false

end )

end )[/lua]