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

What have i done wrong here with my backpack cloning?

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 ;-;

5 comments

0

Any errors?

0

Not that i know of. Its just not cloning

0

Maybe just try using the PlayerAdded event on line 11 instead. Not sure if it'll matter, but that's all that strikes my eye. game.Players.PlayerAdded:connect(onEntered)

0

Okay thanks.

0

Hm, It doesn't change a thing sadly :/ I don't know what else to try, I've tried changing the function and everything but it still doesn't work. Thanks for the help anyway

2 answers

✓ Accepted answer

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

4 comments

0

Oh and I forgot, I recommend you change 'ChildAdded' to 'PlayerAdded'

0

Nope, Nothing's changed.

0

Oh nevermind! I changed the newPlayer.Backpack to the other option you suggested (newPlayer:FindFirstChild('Backpack')) and it worked perfectly! Cheers!

0

Anytime!

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.

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