Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

If statement not working and while loop just restarts?

Asked by
CodeWon 181
3 years ago
Edited 3 years ago

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

Log in to vote
1
Answered by 3 years ago

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
0
Everything works, except for line 17-Attempt to index nil with 'Character' how can I fix this? CodeWon 181 — 3y
0
Well, if this is a server script you can't get the local player MOREHOURSOFFUN 104 — 3y
Ad

Answer this question