Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this 'while true do' function not work properly?

Here is my code.

while true do
	wait(0.5)
	if script.Parent.Owner.Value == "None" then
		return
	else
		wait(0.5)
		local player = game.Players:FindFirstChild(script.Parent.Owner.Value)
		if player.leaderstats.Money.Value > script.Parent.Storage.Value then
			player.leaderstats.Money.Value = script.Parent.Storage.Value
		end
	end
end

This code works at the beginning.

while true do
	wait(0.5)
	if script.Parent.Owner.Value == "None" then
		return

But once script.Parent.Owner.Value is a different then "None", it still stays on the first if area. It doesn't go onto the else area.

Any Help would be Nice! Thanks.

1 answer

✓ Accepted answer

Once you return, the loop ends already. A better way to wait for the value to change is to use an event listener.

script.Parent.Owner.Changed:Connect(function(NewValue)
    if NewValue == "WhateverYouWantItToCheck" then
        local player = game.Players:FindFirstChild(script.Parent.Owner.Value)
        if player.leaderstats.Money.Value > script.Parent.Storage.Value then
            player.leaderstats.Money.Value = script.Parent.Storage.Value
        end
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.