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

My instances are not being created; how come?

Asked by
OniiCh_n 410 Moderation Voter
10 years ago

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

Log in to vote
2
Answered by
duckwit 1404 Moderation Voter
10 years ago

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)
0
This one explained it in detail, but yeh... OniiCh_n 410 — 10y
Ad
Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
10 years ago

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)

Log in to vote
-3
Answered by
Ekkoh 635 Moderation Voter
10 years ago

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.

Answer this question