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

What have i done wrong here with my backpack cloning?

Asked by 8 years ago
Edited 8 years ago

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

0
Any errors? User#11440 120 — 8y
0
Not that i know of. Its just not cloning potato12344 10 — 8y
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) User#11440 120 — 8y
0
Okay thanks. potato12344 10 — 8y
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 potato12344 10 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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

0
Oh and I forgot, I recommend you change 'ChildAdded' to 'PlayerAdded' User#13152 0 — 8y
0
Nope, Nothing's changed. potato12344 10 — 8y
0
Oh nevermind! I changed the newPlayer.Backpack to the other option you suggested (newPlayer:FindFirstChild('Backpack')) and it worked perfectly! Cheers! potato12344 10 — 8y
0
Anytime! User#13152 0 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

Answer this question