Code returns random numbers instead of actual values
4 replies, posted
Making a gamemode in which buttons on the map allow players to buy stuff, using the buttons' keyvalues. But instead of subtracting the cost of the button from the player's money, it replaces the cost with a random number.
Two buttons have the keyvalues 'cost' and '500'. One's value is replaced by 255 and the other is replaced by 338.
[lua]
function GM:EntityKeyValue(ent,key,value)
if (ent:GetClass() == "func_door" or "func_button" or "func_button_rot" or "func_door_rotating") and (key == "cost" or "Cost") then
ent.cost = tonumber(value)
end
end
function usemapitem(ply,ent)
if IsValid(ent) then
if (ent:GetClass() == "func_door" or "func_button" or "func_button_rot" or "func_door_rotating") and ply.points >= ent.cost then
timer.Create("activate"..ent:EntIndex(),0.3,1, function()
ply:TakePoints(tonumber(ent.cost))
ply:ChatPrint(""..ent.cost)
ply:SetNWInt("kpoints",ply.points)
end)
return true
else return false
end
end
end
hook.Add( "PlayerUse", "some_unique_name", usemapitem )
[/lua]
Do
print(ent, ent.cost, tonumber(value) ) in your GM:EntityKeyValue and post results.
[code]function GM:EntityKeyValue(ent,key,value)
if (ent:GetClass() == "func_door" or "func_button" or "func_button_rot" or "func_door_rotating") and (key == "cost" or "Cost") then
ent.cost = tonumber(value)
end
end[/code]
Please read your code again.
[B]ent:GetClass() == "func_door" or "func_button" or "func_button_rot" or "func_door_rotating"[/B] will always return true because string always equals to true.
if class is not equal to "func_door", it will go ahead and see "func_button" as true.
Same goes for [B]key == "cost" or "Cost"[/B]
Wouldn't have figured that out for years. I forgot how picky lua's syntax is. Thanks.
Sorry, you need to Log In to post a reply to this thread.