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

Repeat wait until an object exists?

I want multiple instances to be created ONLY when an object is created in another script. This is the code I came up with.

game.Players.PlayerAdded:connect(function(plr)
	repeat wait() until not plr.storage
            local item = Instance.new("BoolValue", plr.storage)
    end)

I know it's wrong.

The storage object is a Backpack placed inside the Player. Inside it contains a lot of BoolValues for future reference. I want the BoolValues to be created in a separate script just for the sake of organization on my part.

1 comment

0

Why not just do: if something ~= nil then

2 answers

The problem is that you are waiting until that storage doesn't exist. This is proper logic:

game.Players.PlayerAdded:connect(function(plr)
    repeat wait() until plr:FindFirstChild("storage")
            local item = Instance.new("BoolValue", plr.storage)
    end)

A better suggestion than what zars15 suggested is this;

game.Players.PlayerAdded:connect(function(plr)
	local storage = plr:WaitForChild("storage")
	local item = Instance.new("BoolValue", storage)
end)

Look at the wiki - WaitForChild

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