How can I fix this?

I am using scripted entities and wiremod to create a zombie survival game without creating a seperate game mode. The two entities I have are the Killcounter and WiredWeaponDistributor.

Kill Counter
Inputs:
-MoneyPerKill (How much money you earn for each kill)
-Spent1 through Spent9 (Used to input the total spent at each distributer)
Outputs:
-Kills (Total Kills)
-Money (Current amount of money the player has)
WWD
Inputs:
-Weapon (The weapon sold at this distributer)
-Cost (The cost to buy the weapon)
-CurMoney (Current Money)
Output:
-Spent (Total spent at this distributer)

Here’s how it works:

(Note: I used constant values to be able to easily change values without restarting Gmod)

The KillCounter takes counts each kill, saved to the variable kills, output through the kills output, and multiplies this by the MoneyPerKill, input from a constant value, which is set to the variable total money. Total money is subtracted by the total amount of money spent at each distributer to get the current amount of money the player has.

When used, the WWD checks if you have enough money to buy the weapon by comparing the current money to the cost (Input from a constant value). If true, it adds the cost to the total amount spent at this distributer and spawns the weapon specified by a constant value.

The problem is that the WWD spawns several of the weapon before it realizes you do not have enough money. How can I fix this?

Also, is there a way to specify how much ammo each weapon spawns with?

I realize I could probably make this easier using global variables instead of wires, but I want to get it to work with wires before I change it.

Any other ways to simplify the code are welcome.

Init code for KillCounter:

[lua]
–[[
KillCounter created by Sangheili868

Used with the Wired Weapon Distributer to create zombie survival games.

Use constant values to specify how much money you earn per kill.

Wire the Spent inputs to the output of each weapon distributer.
Wire the CurMoney inputs of each weapon distributer to the money output.

Use screens to display the money and kills outputs.

–]]

AddCSLuaFile( “cl_init.lua” ) --Send cl_init.lua to the client
AddCSLuaFile( “shared.lua” ) --Send shared.lua to the client
include( ‘shared.lua’ ) --Include shared.lua

function ENT:Initialize()

self:SetModel( "models/props_lab/servers.mdl" ) 
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
local phys = self:GetPhysicsObject()
self.NextThink = CurTime() +  1
if (phys:IsValid()) then
	phys:Wake()
end

self.Inputs = Wire_CreateInputs(self.Entity, {"MoneyPerKill", "Spent1", "Spent2", "Spent3", "Spent4", "Spent5", "Spent6", "Spent7", "Spent8", "Spent9"})
	--[[
		Creates an input for the money earned per kill(specified by a 
		constant value)
	--]]
self.Outputs = Wire_CreateOutputs(self.Entity, {"Kills", "Money"})
	--Creates outputs for the kills and money
self.FirstInput = 0 
self.SecondInput = 0
self.ThirdInput = 0
self.FourthInput = 0
self.FifthInput = 0
self.SixthInput = 0
self.SeventhInput = 0
self.EighthInput = 0
self.NinthInput = 0
self.TenthInput = 0

end

function ENT:SpawnFunction( ply, tr )
kills = 0
totalmoney = 0
money = 0
if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 36

local ent = ents.Create( "KillCounter" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
return ent

end

hook.Add(“OnNPCKilled”, “OnNPCKilled 0x5AE6CCAE”, function(npc)
kills = kills + 1
end)

–Increases kills for every npc killed

function ENT:TriggerInput(name, value)
if name == “MoneyPerKill” then
self.FirstInput = value
end
if name == “Spent1” then
self.SecondInput = value
end
if name == “Spent2” then
self.ThirdInput = value
end
if name == “Spent3” then
self.FourthInput = value
end
if name == “Spent4” then
self.FifthInput = value
end
if name == “Spent5” then
self.SixthInput = value
end
if name == “Spent6” then
self.SeventhInput = value
end
if name == “Spent7” then
self.EighthInput = value
end
if name == “Spent8” then
self.NinthInput = value
end
if name == “Spent9” then
self.TenthInput = value
end
end

function ENT:Use()
end

function ENT:OnRemove()
Dev_Unlink_All(self:GetTable().conduit)
end

function ENT:Think()
Wire_TriggerOutput(self, “Kills”, kills)
Wire_TriggerOutput(self, “Money”, money)
–Outputs the outputs
totalmoney = kills * self.FirstInput
–Multiplies the number of kills by money per
–kill to get the total monye earned
money = totalmoney - self.SecondInput - self.ThirdInput - self.FourthInput - self.FifthInput - self.SixthInput - self.SeventhInput - self.EighthInput - self.NinthInput - self.TenthInput
–Takes the total money and subtracts from it the total spent at each
–distributer to get the current money
end
[/lua]

And the WWD:

[lua]
–[[
Wired Weapon Distributer created by Sangheili868

Used with the Kill counter to create zombie survival games.

Use constant values to specify the weapon that is sold at this
distributor and its cost.

Wire CurMoney to the Money output of the KillCounter.
Wire the Spent1 (Or whatever shop this is) of the 
KillCounter to this entity's output

–]]

AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
include( ‘shared.lua’ )

function ENT:Initialize()

self:SetModel( "models/props_lab/reciever_cart.mdl" ) 
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
local phys = self:GetPhysicsObject()
self.NextThink = CurTime() +  1
if (phys:IsValid()) then
	phys:Wake()
end

self.Inputs = Wire_CreateInputs(self.Entity, {"Weapon", "CurMoney", "Cost"})
	--[[
	    Creates wire inputs for the Weapon that this entity sells, the 
		amount of money the player currently has, and the cost of the weapon.
	    Weapon and Cost are specified with Constant values, and CurMoney
	    is wired to the money output on the KillCounter
	--]]
self.FirstInput = 0 
self.SecondInput = 0
self.ThirdInput = 0
	--Sets the starting inputs to 0
self.Outputs = Wire_CreateOutputs(self, {"Spent"})
	--Creates an output for the total amount of money spent here.

end

local spent = 0 --Creates a spent variable

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 36

local ent = ents.Create( "WWD" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
return ent

end

function ENT:TriggerInput(name, value)
if name == “Weapon” then
self.FirstInput = value
end
if name == “CurMoney” then
self.SecondInput = value
end
if name == “Cost” then
self.ThirdInput = value
end
end

function ENT:Use()

if self.SecondInput >= self.ThirdInput then

--Checks if you have enough money to buy the weapon.

	spent = spent + self.ThirdInput

--Adds the cost of the weapon to the total spent so far.

--Below spawns the weapon specified by the weapon input
--[[
1-357 (Magnum)
2-crossbow
3-frag (grenade)
4-ar2 (Pulse Rifle)
5-Pistol
6-RPG
7-shotgun
8-smg1
--]]

	if self.FirstInput == 1 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_357") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
		
	elseif self.FirstInput == 2 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_crossbow") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
		
	elseif self.FirstInput == 3 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_frag") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
		
	elseif self.FirstInput == 4 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_ar2") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
	
	elseif self.FirstInput == 5 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_Pistol") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
	
	elseif self.FirstInput == 6 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_RPG") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
	
	elseif self.FirstInput == 7 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_shotgun") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
	
	elseif self.FirstInput == 8 then
	local ply = Entity(1) 
	local tr = ply:GetEyeTrace() 
	ent = ents.Create("weapon_smg1") 
	ent:SetPos(tr.HitPos) 
	ent:Spawn()
	end
		
--Outputs the spent variable to the KillCounter		
Wire_TriggerOutput(self, "Spent", spent)	

end

end

function ENT:OnRemove()
Dev_Unlink_All(self:GetTable().conduit)
end

function ENT:Think()
end
[/lua]

EDIT: thegrb93’s solution didn’t work. Could someone come up with another solution? I updated the code for the WWD.
EDIT2: i found a solution. I simply created a local variable “active” and set it equal to one. A weapon could only be bought if it was equal to 1, and after buying a weapon, it was set to zero. Then a timer began that after 1 second it was set back to 1.

The reason your KillCounter thinks it can buy that many weapons though is because of this.
[lua]
function ENT:Think()
–Outputs the spent variable to the KillCounter
Wire_TriggerOutput(self, “Spent”, spent)
end
[/lua]
That think function updates slowly, not on every game tick. So your killcounter is behind on how much has been spent.

put the Wire_TriggerOutput(self, “Spent”, spent) between lines 154 and 155 so it will update the spent output everytime it is used.

Thanks I’ll try this out.