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

If statement not working and while loop just restarts?

The script I made is supposed to detect when there are no more items in a folder, its in a while true do loop. But the loop completely restarts and doesn't do anything when all the items are collected. Whats wrong?

	while true do -- this loop is inside of another loop
		local Gems = ClonedMap.Gems
		
		local player = game.Players.LocalPlayer
	
		if not Gems:FindFirstChild("Gem") then -- This is supposed to check if there are any items (gems) in the folder
	
			wait(1) 
	
			Status.Value = "All gems have been found, game over!"
	
			wait(1)
			
	
			player.Character:BreakJoints() -- Resets players when all gems are found, this 	part also doesn't work
	
			ClonedMap:Destroy() -- Resets the map

			break
		end
	end

1 answer

✓ Accepted answer

You could try making a variable that the while true do runs on, for example:

local gemsFound = false

while not gemsFound do -- this loop is inside of another loop
    local Gems = ClonedMap.Gems

    local player = game.Players.LocalPlayer

    if not Gems:FindFirstChild("Gem") then -- This is supposed to check if there are any items (gems) in the folder

        wait(1) 

        Status.Value = "All gems have been found, game over!"

        wait(1)


        player.Character:BreakJoints() -- Resets players when all gems are found, this  part also doesn't work

        ClonedMap:Destroy() -- Resets the map

	gemsFound = true
    end
end

2 comments

0

Everything works, except for line 17-Attempt to index nil with 'Character' how can I fix this?

0

Well, if this is a server script you can't get the local player

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