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

Instance.new seems to be the problem?

Asked by
emite1000 335 Moderation Voter
10 years ago

So I'm making a script that changes the player's shirt when they join (and if they don't have a shirt it creates one on them). It prints to 'He has no shirt", but then gives the error "Attempted to index global "S", a nil value.'

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        print("So far so good")
        S = character:FindFirstChild("Shirt")
        if S then
            print("He has a Shirt")
            S.ShirtTemplate = ("http://www.roblox.com/asset/?id=137904320")
        else 
            Instance.new("Shirt", character)
            print("He has no shirt")
            S = character:FindFirstChild("Shirt")
                S.ShirtTemplate = ("http://www.roblox.com/asset/?id=137904320")
        end
    end)
end)

1 answer

Log in to vote
0
Answered by
1N0body 206 Moderation Voter
10 years ago

Try to use this code, at the end of the script

function PlayerAdded(player)
 print("So far so good")
 S =player.Character:FindFirstChild("Shirt")
 if S then
 print("He has a Shirt")
S.ShirtTemplate = ("http://www.roblox.com/asset/?id=137904320")
else 
Instance.new("Shirt", character)
 print("He has no shirt")
S = player.Character:FindFirstChild("Shirt")
 S.ShirtTemplate = ("http://www.roblox.com/asset/?id=137904320")
  end
 end
end

for _, player in pairs(game.Players:GetPlayers()) do
    PlayerAdded(player) -- Use your player added handling function here.
end

At line 08 instead of "if nil then" use "else", and then delete line 12 'end' like in the code given.

0
I'm sorry, there was a mistake on my end and I posted the wrong stuff in my question (the script I have in there now is the correct script I am actually using). It turns out my real problem was that the Instance.new isn't working, not the CharacterAdded. emite1000 335 — 10y
Ad

Answer this question