What do you want to achieve?
I want that if the number Value is greater than 0 it damages the player and if it’s = to 0 it heals the humanoid.
What is the issue?
The while loop doesn’t seem to work, and I do not know why. I have a number value being passed to this serverscript, maybe something’s up with that.
ServerScript stored inside Model
game.ReplicatedStorage.PassTimer.OnServerEvent:Connect(function(player, number)
Humanoid = player.Character:WaitForChild("Humanoid")
Value = number
end)
while true do
if Value > 0 then
wait(2)
Humanoid.Health = Humanoid.Health - 10
elseif Value <= 0 then
wait(1)
Humanoid.Health = Humanoid.Health + 8
end
end
LocalScript that passes the Value:
timer:GetPropertyChangedSignal("Value"):Connect(function()
script.Parent.Amount.Text = timer.Value
local number = timer.Value
game.ReplicatedStorage.PassTimer:FireServer(number)
end)
Feel free to suggest better solutions if you think that it can be done better!
I think the while true loop is running, it’s just that there’s no way for it to get the “value” variable. An easy solution could be recording the value through a string value so the while loop can access it.
Yeah this was my though too honestly, this was the reason why I removed the local before the Value cuz I thought the while loop could refer to it that way but doesn’t seem to work
Wait, now that I think about it, an Int value works better. To create them, you can go to workspace and rightclick to insert an object and search for it. All you need to do after that is make a variable for it in whatever script needs it and then record the number.
IntValueName.Value = number
If you take a look at the script, I am using an IntValue already, I just decided to pass the value over because I am not sure if the server script is able to read the IntValue since it’s set by a localscript, you know?
Have you tried putting the if statement inside of the OnServerEvent connection?
It’d look like this:
game.ReplicatedStorage.PassTimer.OnServerEvent:Connect(function(player, number)
Humanoid = player.Character:WaitForChild("Humanoid")
Value = number
if Value > 0 then
wait(2)
Humanoid.Health = Humanoid.Health - 10
elseif Value <= 0 then
wait(1)
Humanoid.Health = Humanoid.Health + 8
end
end)
Yeah the local script fires the event, and the server side recives it as it deals with humanoid damage. Humanoid damage is not being replicated automatically from a localscript so I am forced to replicate it
You could add a debounce/cooldown to the if statement in the remote event:
local debounce = false
game.ReplicatedStorage.PassTimer.OnServerEvent:Connect(function(player, number)
Humanoid = player.Character:WaitForChild("Humanoid")
Value = number
if not debounce then
debounce = true
if Value > 0 then
wait(2)
Humanoid.Health = Humanoid.Health - 10
elseif Value <= 0 then
Humanoid.Health = Humanoid.Health + 8
end
end
end)
There is a health script running for the player that already adds health a bit at a time. Anything you do with health other than = 0 will seem to have seemingly odd results.