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

Instance.new seems to be the problem?

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

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.

1 comment

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.

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