Loop Continuously Running

Hello,

I’m trying to achieve a way that players will get damage if their value is higher than 75 and lower than 25.

I’ve tried using coroutine and switching the position of the loop and this is what I’ve came so far. However, it not printing true for some reason. I’ve ran out of idea as this point so any suggestions would be a blessing.

local maxValue = 100

local ColdEffect = game:GetService("ReplicatedStorage").Assets:WaitForChild("ColdEffect")
local HotEffect = game:GetService("ReplicatedStorage").Assets:WaitForChild("HotEffect")

local NewColdEffect
local NewHotEffect

game.Players.PlayerAdded:Connect(function(player)
	local data = Instance.new("Folder")
	data.Name = "Data"
	data.Parent = player

	local temp = Instance.new("NumberValue")
	temp.Value = 50
	temp.Name = "Temp"
	temp.Parent = data
	
	task.wait(1)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local tempData = player.Data:WaitForChild("Temp")
	local damagePerSecond = 50
	local willLoop = false

	player.Data:WaitForChild("Temp").Changed:Connect(function()
		
		math.round(tempData.Value)
		
		if tempData.Value >= 25 or tempData.Value <= 75 then
			willLoop = false
			print(willLoop, tempData.Value)
		else
			print("huh")
			willLoop = true
			
		if character.HumanoidRootPart:FindFirstChild("FlameHit") then
			NewHotEffect:Destroy()
		elseif character.HumanoidRootPart:FindFirstChild("WaterHit") then
			NewColdEffect:Destroy()
			end
		end
		
		if tempData.Value < 0 then
			tempData.Value = 0
		end

		if tempData.Value > maxValue then
			tempData.Value = 100
		end
	end)
	
	while willLoop == true do
		if tempData.Value <= 25 then
			NewHotEffect = HotEffect:Clone()

			NewHotEffect:Clone()
			NewHotEffect.Parent = character:WaitForChild("HumanoidRootPart")
			NewHotEffect.CFrame = character:WaitForChild("HumanoidRootPart").CFrame

			local Weld = Instance.new("Weld")
			Weld.Parent = NewHotEffect

			Weld.Part0 = NewHotEffect
			Weld.Part1 = character:WaitForChild("HumanoidRootPart")
			Weld.C0 = CFrame.new(0, 0, 0)

			while tempData.Value <= 25 do
				task.wait(1)
				humanoid:TakeDamage(damagePerSecond)
				if tempData.Value >= 25 then
					break
				end
			end
		elseif tempData.Value >= 75 then
			NewColdEffect = ColdEffect:Clone()

			NewColdEffect:Clone()
			NewColdEffect.Parent = character:WaitForChild("HumanoidRootPart")
			NewColdEffect.CFrame = character:WaitForChild("HumanoidRootPart").CFrame

			local Weld = Instance.new("Weld")
			Weld.Parent = NewColdEffect

			Weld.Part0 = NewColdEffect
			Weld.Part1 = character:WaitForChild("HumanoidRootPart")
			Weld.C0 = CFrame.new(0, 0, 0)

			while tempData.Value >= 75 do
				task.wait(1)
				humanoid:TakeDamage(damagePerSecond)
				if tempData.Value <= 75 then
					break
				end
			end
		end
	end


	humanoid.Died:Connect(function()
		task.wait(game.Players.RespawnTime)
		player.Data.Temp.Value = 50
	end)
end)

Thank you! :slight_smile:

1 Like

Is “huh” printing? I think the problem is that since at first the willLoop is false, the while loop will check if its true or false, and when it sees that it is false, it will instantly stop it, even if it becomes true later.

1 Like

No that’s the problem. It not printing when condition is met. How can I make it not stop when it became true again?

Sorry, I can’t understand what you mean, do you mean that it is never changing willLoop from false to true?

Yes, the conditional is met and it not changing to that.

One thing I noticed is you are defining the variable “temp” twice,
In here:

local temp = Instance.new("NumberValue")

And here:

local tempData = player.Data:WaitForChild("Temp")

You can probably remove the last one since that might mess things up.
In here:

player.Data:WaitForChild("Temp").Changed:Connect(function()

You can simply use tempData instead of finding it again.
Before the if statements, try to print(tempData.Value), to see if it actually meets the conditions.

Alright, I changed the variable.

Yeah, it does print because it meets the first condition. But doesn’t do the second one when condition is no longer met. Maybe because of debounce or something I don’t know.

I don’t see any debounces, so I am not sure if that’s really the problem. However, you are using alot of while loops inside each other, so I recommend trying to find a way to just use a single loop to check everything.

Well, I kinda need the while loops to continuously check and damage them. So I don’t think that possible; and it wouldn’t fix anything.

Don’t just print variables, or things like “huh” after a condition is met. That only tells you if it worked.

Print the variable you are checking before the if, that way if it isn’t printing what you’d expect then you can troubleshoot that part of the script.

For example when you put if x = false then as your check put print(x) just before it. It will tell you exactly what x is and if for some reason x prints as nil and not true or false, it tells you you have to do some checking wherever x is set.

Got it, it seems like issue with the “or” statement. I’ll continue troubleshooting.

1 Like

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