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

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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.

Ad

Answer this question