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
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.
local ball = game.Workspace:WaitForChild("ball") -- The object named "ball"
local plate = game.Workspace:WaitForChild("plate") -- The object named "plate"
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