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

How can I fix this script that awards items to players who have badges?

In a game that I have made, I wanted to award players who have received badges by putting tools into their starter pack next time they join, I have a script that should work for this but when I go into my game I don't get anything, I looked through the script dozens of times and compared it to similar scripts to see what was wrong but I still can't spot the problem, If anyone can help me then I would be most grateful. Here is the problematic script

local BadgeService = game:GetService("BadgeService")
local BadgeId = 12312321
 
game:GetService("Players").PlayerAdded:connect(function(player)
    if BadgeService:UserHasBadge(player.UserId, BadgeId) then
        local tool = game.ServerStorage["Dream Bat"]
			tool:clone().Parent = player.Backpack
			tool:clone().Parent = player.StarterGear
   
    end
end)

1 answer

✓ Accepted answer

Hi!

The issue with this script is that the function will run before Backpack and StarterGear has time to load. This is because it is a PlayerAdded event. For this, you will need to put a WaitForChild() Instance on both parents for the tool.

local BadgeService = game:GetService("BadgeService")
local BadgeId = 12312321

game:GetService("Players").PlayerAdded:connect(function(player)
    if BadgeService:UserHasBadge(player.UserId, BadgeId) then
        local tool = game.ServerStorage["Dream Bat"]
            tool:Clone().Parent = player:WaitForChild("Backpack")
            tool:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

Note: Make sure this is not in a LocalScript, since LocalScripts run client side. The script won't work if it is in one.

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