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)
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.