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

My shirt giver breaks when I try to add a characteradded function?

My script doesnt seem to work, and im 99.9% positive its something on my end im doing wrong. heres the script working, anyone help with adding it so when a character loads to, they get it?

local shirtId = script.Parent.Parent.Coathanger.Shirt.ShirtTemplate
local cPart = script.Parent

-- Functions --

local function playSoundLocal(sId,sParent)
	local sound = Instance.new("Sound",sParent)
	sound.SoundId = "http://www.roblox.com/asset/?id="..sId
	sound:Play()
	sound:Destroy()
end

game.Players.PlayerAdded:connect(function(player)
repeat wait() until player.Character
if player.leaderstats.Money12.Value>= 1 then
	print(player.Name.." clicked on Uniform Giver")
	local foundShirt = player.Character:FindFirstChild("Shirt") -- Tries to find Shirt
	if not foundShirt then -- if there is no shirt
		print("No shirt found, creating for "..player.Name)
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
	else if foundShirt then -- if there is a shirt
		print("Shirt found, reconstructing for "..player.Name)
		player.Character.Shirt:remove()
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
		player.Character.Shirt.ShirtTemplate = shirtId
	end
	end
end
end)

2 comments

0

Any errors in the output?

0

No. It just doesn't happen.

1 answer

There are a few possibilities:

  1. This is a Script placed somewhere only a LocalScript will run
  2. This is a LocalScript, in which case you shouldn't be using game.Players.PlayerAdded -- just access the local player using game.Players.LocalPlayer

The local player is already added to the game before a LocalScript will run, meaning you're telling the script to wait until another player enters the game.


(Edit - ah, it's a script? I was confused because the one function is called playSoundLocal)

The problem is that your if statement on line 15 only runs once - when the player's character spawns in for the first time.

To change this, you need to listen to the "CharacterAdded" event (instead of using a repeat wait() until player.Character loop):

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if player.leaderstats.Money12.Value >= 1 then
			print(player.Name.." clicked on Uniform Giver")
			local shirt = character:FindFirstChild("Shirt") -- Tries to find Shirt
			if shirt then
				print("Shirt found, destroying for " .. player.Name)
				shirt:Destroy()
			end
			print("Creating shirt for " .. player.Name)
			shirt = Instance.new("Shirt")
			shirt.Name = "Shirt"
			shirt.ShirtTemplate = shirtId
			shirt.Parent = character
		end
	end)
end)
-- I don't think you need to destroy the shirt - can't you repurpose it? (I haven't tested)
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if player.leaderstats.Money12.Value >= 1 then
			print(player.Name .. " clicked on Uniform Giver")
			local shirt = character:FindFirstChild("Shirt")
			if not shirt then
				shirt = Instance.new("Shirt")
				shirt.Name = "Shirt"
				shirt.Parent = character
			end
			shirt.ShirtTemplate = shirtId
		end
	end)
end)

Note that there are 2 versions of the code there, only use one of them. I also modified a few things:

  • remove is deprecated, use Destroy
  • I replaced player.Character with character since character is provided by the CharacterAdded event
  • Instance.new(type, parent) is deprecated - that is, you aren't supposed to provide the parent argument anymore
  • I simplified the if and what to do in each case; the code that you want to do regardless of if the player has a shirt or not can be taken out of the if statement so you don't have to repeat yourself

1 comment

0

It is a script, and im trying to get it so that it gives the player the shirt when they spawn/respawn, not only when they first spawn.

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