Need some sort of advanced debugger.. strange issue
10 replies, posted
I've set up a stamina system so that players cannot sprint endlessly. They will sprint until they have no stamina, then it will drain their health. They could sprint until death, I suppose.
When a player joins, their stamina is set to 15. There's a think hook that will slowly regenerate it. It also checks to see if the player is sprinting and that will degenerate it.
The problem is, for some reason, every few seconds the stamina is reset to its initial value of 15. I tried starting it at 0 and it resets back to 0. This is not helpful at all.
I have many variables initialized in "GM:PlayerInitialSpawn( ply )", all of which work fine.. except for the stamina. The only time that variable is touched is when the player is sprinting, and it's set up like (Stamina = Stamina -1).
I have no idea how this is happening, and the only way to debug it is for me to put "Msg(Stamina)" everywhere so I can see exactly when it happens. So far, I've seen no correlation.
I don't know how useful this code will be as I have to edit out most of it so I can just show the parts that matter.
This is in init.lua
[CODE]
function GM:PlayerInitialSpawn( ply )
ply:SetNetworkedInt("Stamina", 0)
ply:SetNetworkedInt("MaxStamina", 15)
end [/CODE]
This is in shared.lua, under the client section
[CODE] function doThink()
ply = LocalPlayer()
if( ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_SPEED )) then
if (ply:GetNetworkedInt("Stamina") > 0) then
ply:SetNetworkedInt("Stamina", ply:GetNetworkedInt("Stamina")-.03)
Msg(" ... "..ply:GetNetworkedInt("Stamina").." ... ")
else
ply:SetHealth(ply:GetHealth()-1)
Msg(ply:Health())
end
else
ply:SetNetworkedInt("Stamina", ply:GetNetworkedInt("Stamina")+.01)
end
end
hook.Add("Think", "doThink", doThink)[/CODE]
So if the player is sprinting and their stamina is greater than 0, reduce stamina by .03. If the stamina is 0, reduce health by 1. If the player isn't sprinting, increase stamina by .01.
I feel like I'm grasping at straws here asking for help, but any advice is appreciated.
I'm not sure, but you might want to decrament stamina in a seperate hook that is called when a key is held. Also, you might want to check the player's hp is positive, i think if health<0 the player crashes.
[QUOTE=bobthe2lol;22642298]I'm not sure, but you might want to decrament stamina in a seperate hook that is called when a key is held. Also, you might want to check the player's hp is positive, i think if health<0 the player crashes.[/QUOTE]
Well I put in the think hook so that it was always checking. Otherwise it will only decrement once. I need it to decrement constantly.
And if health <0, the player can't sprint.. cuz he's dead...
No, hes not dead. Setting a player's health below 0 does not kill them.
[editline]05:02PM[/editline]
Also if you noticed, i said a keydown hook, not a keypress hook.
You can't add a float to an integer.
Yes to add on what Crazy Quebec said, you have stored Stamina as an int which can't contain decimals but you try to make it a decimal which doesn't work
Huh. Ok. I didn't think it actually mattered. I've used "GetNetworkedString" and accidentally called an int value and it displayed the int. And vice versa. I changed them to floats, no change in the outcome though.
It resets approximately every 10 seconds, regardless of what "stamina" is currently set to. It happens at exact intervals.. seemingly about 10 seconds.
Edit:
And it's probably worth mentioning that even when stamina < 0, the health never decrements.
I still really need help with this. Why would a variable reset itself? And it's definitely something to do with time... pretty much every 10 seconds.
You're calling SetNW* client-side, which doesn't work. The variable resets every time server re-sends it.
[QUOTE=raBBish;22671573]You're calling SetNW* client-side, which doesn't work. The variable resets every time server re-sends it.[/QUOTE]
Exactly. If it wasn't that way the the clients would simply have however much stamina they want.
Huh.. Ok, now we're gettin somewhere!
Now my problem is if I'm running it serverside, then it will constantly have to be looping through every player, right?
Edit:
Hah.. wow.. added a loop to cycle through players, worked amazingly. Health still doesn't degenerate but that's probably something silly
Thanks a TON!!!!
Sorry, you need to Log In to post a reply to this thread.