While loop doesn't work at all?

  1. 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.

  2. 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!

Oh and yeah, obviously I can’t put the while loop into the function above, because otherwise the damage would just stack up.

I don’t recommend using while true do, because it could crash your device, but instead, use while wait() do. That’s because true has no delay.

1 Like

I shall edit that, but unfortunately it doesn’t have an impact on the error I mentioned in the description. Thank you for your advice tho!

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.

1 Like

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

Oh could you elaborate further? What do you mean by recording the value through a string value? Thank you for responding too btw!

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

1 Like

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?

Hello,

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)
1 Like

But the local script is to fire that remote event right? On the serverside, the IntValue should be set.

1 Like

Yes I did, problem is just that the Event gets fired quite often, therefore the amount of damage stacks up.

Try and remove the wait(2) then

1 Like

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)
1 Like

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.

1 Like

Oh wow that actually works, thank you very much!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.