Help With Giving Player Items When They Join

I am trying to give players items when they join, but it won’t work.
Here is my original script:

game.Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 2 then
		local mop = game.ServerStorage.Items.Mop
                local mopCopy = mop:Clone()
                mopCopy.Parent = plr:WaitForChild("Backpack")
	end
end)

That didn’t work, but it didn’t show any errors, so I tried this:
Server:

game.Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 2 then
		game.ReplicatedStorage.FireItems.FireMop:FireClient(plr)
	end
end)

Client:

local mop = game.ReplicatedStorage.Mop

game.ReplicatedStorage.FireItems.FireMop.OnClientEvent:Connect(function(plr)
	local mopcopy = mop:Clone()
	mopcopy.Parent = plr:WaitForChild("Backpack")
end)

The new error is that it is attempting to index nil with wait for child. I changed the location of the item after the first script didn’t work, so instead of server storage, it was in replicated storage. How do I fix this?

This should work, add a print statement right after the if statement

2 Likes

It’s still not working. It’s really wierd because the print statement ran, but the item wasn’t in my backpack.

What does this print?
Also you can remove that and change the next line to:


local mopClone = game.ServerStorage.Items:WaitForChild("Mop"):Clone()
1 Like

Oh I figured out the issue. The player wasn’t fully loaded in, so it wouldn’t work. I just added a wait statement, so it worked, but is there a way where it would just wait until the player was fully loaded in instead of waiting a set amount of time?

Remove the wait() in the beginning of the script because depending ln the quickness/slowness of the computer this can fail.

Instead replace it with:

if not game:IsLoaded() then
    game.Loaded:Wait()
end
1 Like

Now, it won’t get past that if then statement.

Maybe you could make a wait until the humanoidrootpart loads in, when it loads proceed with the script?

Found a link to a post that covers some solutions related to waiting for the character to load: Best way to Wait() for Character?

1 Like

How would I apply this to my script?

Try to add this in the Server Script part, right before the GetRankInGroup line:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait() --If player.Character is nil then it will wait for CharacterAdded event to fire because of :Wait()

Sorry for any weird format, not on my PC rn. Let me know if it works :slight_smile:

Edit: If it doesnt work you can try this too:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait() --If player.Character is nil then it will wait for CharacterAdded event to fire because of :Wait()
local humanoid = character:WaitForChild("Humanoid")
if humanoid then
--Put the rest of your server script here and add another end at the end.