Hello I need a help with a script

I want to make a script that makes a server give badge to the player once 2 object collide, they both have a name, ball and plate. (ball is massless) The issue with my previous script is that it doesn’t award any badge, though I checked console, no message whatsoever. I tried rewriting it sometimes, but it didn’t help. Hopefully someone can find a problem to fix it. (it’s script, and its stored in workspace).

local badgeId = INSERT_BADGE_ID_HERE -- Replace INSERT_BADGE_ID_HERE with the ID of the badge you want to award
local ball = game.Workspace:FindFirstChild("ball") -- The object named "ball"
local plate = game.Workspace:FindFirstChild("plate") -- The object named "plate"
local badgeService = game:GetService("BadgeService")

-- Function to award badge when triggered
local function awardBadge()
    local player = game.Players:GetPlayerFromCharacter(ball.Parent)
    if player then
        local success, errorMessage = pcall(function()
            badgeService:AwardBadge(player.UserId, badgeId)
        end)
        if not success then
            warn("Failed to award badge: " .. errorMessage)
        end
    end
end

-- Connect the function to the Touched event of the "ball" object
if ball and plate then
    local connection
    connection = ball.Touched:Connect(function(hit)
        if hit.Name == "plate" then
            awardBadge()
            connection:Disconnect() -- Disconnect the event listener after the badge is awarded
        end
    end)
else
    warn("Object named 'ball' or 'plate' not found in Workspace!")
end
1 Like
local player = game.Players:GetPlayerFromCharacter(ball.Parent)

You define ball as a child of game.Workspace (line 2) and unless its ancestry changes between the script execution and when the event fires, its parent will still be game.Workspace.

You then try to pass game.Workspace into Players:GetPlayerFromCharacter which returns nil because game.Workspace is not a character.

1 Like

have you checked your inventory, you might have already gotten the badge, so its not giving it to you again

I don’t think it works. Well it definetly doesn’t work


for instance, here’s console, Idk what’s wrong, I’m definetly not a scripter

Try doing instead

local ball = game.Workspace:WaitForChild("ball") -- The object named "ball"
local plate = game.Workspace:WaitForChild("plate") -- The object named "plate"
1 Like

make sure you change the badge id var

Which player? All players on the server?


I don’t think that the ball’s Parent is a player’s Character because of the code below.

In that case the ball’s Parent is Workspace

You need to pass in the players’ character.

local badgeId = INSERT_BADGE_ID_HERE -- Replace INSERT_BADGE_ID_HERE with the ID of the badge you want to award
local ball = game.Workspace:FindFirstChild("ball") -- The object named "ball"
local plate = game.Workspace:FindFirstChild("plate") -- The object named "plate"
local badgeService = game:GetService("BadgeService")

-- Function to award badge when triggered
local function awardBadge(char)
    local player = game.Players:GetPlayerFromCharacter(char)
    if player then
        local success, errorMessage = pcall(function()
            badgeService:AwardBadge(player.UserId, badgeId)
        end)
        if not success then
            warn("Failed to award badge: " .. errorMessage)
        end
    end
end

-- Connect the function to the Touched event of the "ball" object
if ball and plate then
    local connection
    connection = ball.Touched:Connect(function(hit)
            awardBadge(hit.Parent)
            connection:Disconnect() -- Disconnect the event listener after the badge is awarded
    end)
else
    warn("Object named 'ball' or 'plate' not found in Workspace!")
end