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

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

Asked by 5 years ago

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)
0
Instead of destroying their current shirt and then cloning another one, I would suggest just changing the shirt link inside of the Shirt. superawesome113 112 — 5y
1
I tried that, but it still hadn't worked, any suggestions? Pooglies 7 — 5y
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. DaBrainlessOne 129 — 5y
1
I think the shirt "lol" is a diffrent shirt Fad99 286 — 5y
View all comments (3 more)
1
try moving lines 13 - 15 in the char added event Fad99 286 — 5y
0
lol is a different shirt, and ill try what you said, 1 second. Pooglies 7 — 5y
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. Pooglies 7 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
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. Pooglies 7 — 5y
0
Sadly though, this hasn't worked. Pooglies 7 — 5y
Ad

Answer this question