I've wanted to clone this into the players backpack when they spawn but it isn't working, Can someone help me out and tell me why?
adminName = "potato12344" -- I was using my name for testing function onEntered(newPlayer) if newPlayer.Name == adminName then local nitro = game.ReplicatedStorage.Nitro -- Nitro is the name of the weapon and IS correctly spelt local newnitro = nitro:Clone() newnitro.Parent = newPlayer.Backpack end end game.Players.ChildAdded:connect(onEntered)
bump one ;-;
Well, you see, the 'PlayerAdded' (or 'ChildAdded' in your case) event is straight away fired when a player joins the game, meaning the script doesn't wait for the children inside.
What I do recommend is waiting for the Backpack, which can be done so by using the following code:
repeat wait() until newPlayer:FindFirstChild('Backpack') -- I do believe you can just use 'newPlayer.Backpack' aswell
Note, the above code can be written in different ways (eg. using 'WaitForChild' or, a less cleaner way, while not newPlayer.Backpack do
)
You can add this wait either before or after your line 4, so it would ultimately look like this:
adminName = "potato12344" -- I was using my name for testing function onEntered(newPlayer) repeat wait() until newPlayer.Backpack if newPlayer.Name == adminName then local nitro = game.ReplicatedStorage.Nitro -- Nitro is the name of the weapon and IS correctly spelt local newnitro = nitro:Clone() newnitro.Parent = newPlayer.Backpack end end game.Players.ChildAdded:connect(onEntered)
And there you are. If you still have a problem, just let me know. Good luck.
Please upvote if I helped
PS. I do recommend cloning to StarterGear aswell so that the player has it even after dying
When the player connects they may not have a character (and thus a backpack) yet. Use Player.CharacterAdded
event to check for when the player's character has respawned. It will also allow the code to work when the player respawns, instead of just the initial spawn.
Also, change Line 11 to Players.PlayerAdded
.