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

My instances are not being created; how come?

I am basically trying to create a storage of values within the character of a player. This storage's values will probably be remembered via DataStore, but for now it doesn't save. Below is my code in a LocalScript, which is a child of Workspace.

The BoolValues are going to be edited from outside scripts, such as clicking on a brick will change a certain BoolValue. For some reason though, the Character never gets a backpack spawned in it, let alone a BoolValue...

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

EDIT: There is no output given... none at all.

3 answers

✓ Accepted answer

By definition, that is, according to the Official Roblox Wiki, the PlayerAdded event does not fire in a LocalScript - you need to use ChildAdded instead.

Also, take into account what Azarth and Ekkoh said (though there answers won't solve your problem standalone) that player.Character is likely to be undefined before the CharacterAdded event has fired, you can rewrite that bit like-so:

game.Players.PlayerAdded:connect(function(plr)
   -- local char = plr.Character Remove this line
    plr.CharacterAdded:connect(function(char) --receive the Character reference
            local storage = Instance.new("Backpack", char)
            local item = Instance.new("BoolValue", storage)
    end)
end)

What you need to do, then, is either:

  • Run this code in a regular Script object

  • Write it LocalScript style

If you want this to work as a LocalScript you just need to wait for LocalPlayer, and LocalPlayer.Character:

while not game.Players.LocalPlayer do wait() end
while not game.Players.LocalPlayer.Character do wait() end
local storage = Instance.new("Backpack", game.Players.LocalPlayer.Character)
local item = Instance.new("BoolValue", storage)

1 comment

0

This one explained it in detail, but yeh...

Char hasn't been loaded yet.

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

Like Azarth said, when you say

	local char = plr.Character

since the character isn't loaded yet, you just basically set char to nil. Because of that, you're setting the parent of storage to nil.

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