Need help - script that, when players owns a badge, gives them a tool

I have a game with around 15 badges where I want to make it so, when a players gets said badge, they also get a tool in their backpack, specifically tools located inside a folder in server storage


does anyone know how a script like this would work?

4 Likes

A script like that would work like this. Compare the badge id to the tools in the table and if they match, give the tool to the player.

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local ServerStorage = game:GetService("ServerStorage")

local ToolFolder = ServerStorage:WaitForChild("BadgeRewards")
local ToolToBadgeList = {
    BackroomsPoster = 12345678,  -- put the actual badge id here
    -- and so on for each of your tools
}

local function giveToolsForBadges(player)
    for toolName, badgeId in pairs(ToolToBadgeList) do
        local success, hasBadge = pcall(function()
            return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
        end)

        if success and hasBadge then
            local tool = ToolFolder:FindFirstChild(toolName)
            if tool and not player.Backpack:FindFirstChild(toolName) then
                tool:Clone().Parent = player.Backpack
            end
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        giveToolsForBadges(player)
    end)
end)

1 Like

do i add this as a normal script and where?
sorry to ask this too i just really dont know anything about coding lol

this script would probably go in ServerScriptService

1 Like

Learn scripting please it will make your life easier by a ton also, you won’t be able to make a good game without you knowing how to script
at least learn the basics

2 Likes

they’re a clothing designer, they’re likely just making a homestore or something and that doesn’t require much programming

1 Like

literally this lol, it is a homestore

ive tried placing it in the game and it just didnt work, copy and pasted and added the badge id’s to their place and nothing..

oh I didn’t notice that in that case scripting isn’t really required but still learning is preferred

this only works when a player joins not when they are in the game

make sure your script is in the right place, and that you changed the key of each entry (underlined in the image) to the tool’s name
image

more importantly, add this to the bottom of your script. this will handle if any players are already in the game when the script loads.

for _, player in pairs(Players:GetPlayers()) do
    giveToolsForBadges(player)
end
2 Likes

i will give this a check tomorrow, its 2 am

1 Like

omg it worked, tysm for u two!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.