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

My shirt spawner script isn't working on spawn, any help?

My script that with a value, you get a shirt, isn’t working, I have no idea if its because i have 2 leaderstats, instead of just 1, but any help? Heres the script.

game.Players.PlayerAdded:connect(function(player)

player.CharacterAdded:connect(function(character)

if player:WaitForChild("leaderstats").Money12.Value>= 1 then

print("player.leaderstats.Money12.Value")

character.Shirt:Destroy()

end

lol = script.Parent.Shirt:Clone()

lol.Parent = character

end)

end)

7 comments

0

Instead of destroying their current shirt and then cloning another one, I would suggest just changing the shirt link inside of the Shirt.

1

I tried that, but it still hadn't worked, any suggestions?

1

Iunno seems to me like you're destroying the shirt, and then cloning it... which you can't do. Also why is "print("player.leaderstats.....") " in quotes? If you wanted to print the value then you'd best get rid of those quotes.

1

I think the shirt "lol" is a diffrent shirt

1

try moving lines 13 - 15 in the char added event

0

lol is a different shirt, and ill try what you said, 1 second.

0

Moving lines 13 and 15 under character wouldn't work because it hasn't yet checked if the player has the value, giving the shirt to the player without it.

1 answer

There are a few potential issues here. I want to enumerate them and I'll finish the post with suggested changes.

  1. It's highly suggested to throw the shirt into server storage instead. That way if you ever want to change the location of the script (I highly suggest the server script service) then it will be possible without changing the source. This has the additional benefits that traversing the object tree will be a lot more predictable (we will know for sure where the shirt is).
  2. It's not enough to wait for the character to be added to the workspace. Often times, especially if there's a lag spike, the character will be added AND THEN appearance objects like shirts, pants, and hats will trickle in and be placed into the player. I think you should switch from CharacterAdded to CharacterAppearanceLoaded (I can't directly link the page since it's broken to me but it's visible in the player events list).
  3. Shirts are not super unique objects. A lot of their behavior is actually defined by the ShirtTemplate property. This means that we can actually just copy the ShirtTemplate property of your object and paste it into the players shirt. We don't need to do any deletion. This does add additional complexity however and this is explained by the next point.
  4. We don't know IF the player actually has a shirt at all. Default characters I believe do not have a shirt at all. They only have coloring of body parts. This means we will want to make an if statement.
  5. Finally, I want to ask because its not clear from the script. Do you want a person to have a shirt if money12 is above 1 on respawn? Because that's the current behavior. We have to use more events if you want the shirt replaced as soon as money12 has a value above 1.

I have assumed that objects have stayed in the same spot with these edits here. Though I highly suggest implementing 1 and 2 yourself.

-- I'm throwing this in a variable so its easier to edit in the future. 
-- Its a good practice to put "constants" or values that will basically stay
-- the same but are specific here.
local shirt = script.Parent.Shirt
	
game.Players.PlayerAdded:connect(function(player)
		-- I have changed the event here.		
		player.CharacterAppearanceLoaded:connect(function(character)

		-- I have split up the leaderstats check because I
		-- think this may prove useful in the future. This 
		-- may for example be replaced with a changed event 
		-- for the money12 value.
		if player:FindFirstChild("leaderstats") then
			if player.leaderstats.Money12.Value >= 1 then
				if character:FindFirstChild("Shirt") then
					character.Shirt.ShirtTemplate = shirt.ShirtTemplate
				else
					local newshirt = Instance.new("Shirt")
					newshirt.Name = "Shirt"
					newshirt.ShirtTemplate = shirt.ShirtTemplate
					newshirt.Parent = character
				end
			end
		end
	end)
end)

2 comments

0

On your number 5, you are correct, I want it that if a player respawns, and joins in, to get the shirt if they have a Money12 value of 1.

0

Sadly though, this hasn't worked.

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