This script only works PART of the time

Hey guys, this script works sometimes, but not always. I have this snippet 3 times in a LocalScript, but sometimes when I join, none of the three doors get removed (when I touch all three) even though I have enough Wins.

workspace:WaitForChild("BallPitDoor").Touched:Connect(function()
     if game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Wins").Value >= 1 then
          workspace.BallPitDoor:Destroy()
     end
end)

Thanks!

1 Like

Is there more than one thing called workspace.BallPitDoor? As far as I can tell, that should be the only thing preventing this from working.

1 Like

You can always use prints to troubleshoot your code, like this:

workspace:WaitForChild("BallPitDoor").Touched:Connect(function()
     wins = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Wins").Value -- just sets the variable so you only have to set it once

     print("Wins = ", wins)  -- tells you if your door Touched is firing and the number of Wins there are
     -- this also tells you if the next section of code should run
     if wins >= 1 then
          workspace.BallPitDoor:Destroy()
     end
end)
2 Likes

Nope! There’s one BallPitDoor only.

The issue turned out to be a WaitForChild before all of these things that was yielding and preventing the code from reaching, and that WaitForChild was sometimes finishing. My new question is: why does this sometimes yield infinitely?

local wins = player:WaitForChild("leaderstats"):WaitForChild("Wins").Value

(The issue is resolved but I’m still curious)

Edit: it’s not resolved! Still welcoming suggestions.

local wins = player:WaitForChild(“leaderstats”):WaitForChild(“Wins”).Value

This yields until an instance of base class Value (probably an IntValue) named Wins exists in the player’s instance and its value can be read. The only reason this would yield is if Wins didn’t exist yet.

Try following the creation of the Wins instance and see what happens

1 Like

Once more for the dumb guy? (Please elaborate lol)

I ended up changing my system, so I’ll mark this as resolved.

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