I want to award a player a badge when they touch a part.
I went to test it and it didn’t work. Luckily I opened the output tab because one of the scripts I used had a print function and I saw an multiple times error:
local part = script.Parent
local badgeService = game:GetService("BadgeService")
local badge = 2140926703
part.Touched:Connect(function(plr)
local hasBadge = badgeService:UserHasBadgeAsync(plr.UserId, badge)
if hasBadge then
print( plr.Name .. " owns this badge!")
else
badgeService:AwardBadge(plr.UserId, badge)
end
end)
I looked on here but I didn’t find anything to help with this. I’m not sure what’s happening here.
Your plr in this case isn’t the player, it’s the part that touched the part, so in this case plr would be a limb of your character if you touched it
Typically what you can do is get the character via getting the parent of the part (plr.Parent, recommend you rename plr since it’s not a player), then using Players:GetPlayerFromCharacter(), passing in the character as the function says, to confirm that a player touched the part, if they did, award the badge
Right so, firstly plr should be renamed to something else for clarity, I recommend hit as that’s what most use
When we make our character hit the part, hit will be a limb, like a leg for example, hit.Parent gives us the character, we pass the character to the function I mentioned, which returns a player instance if what we gave is a character, or nil if it isn’t, make sure if it’s nil we don’t continue on the code, and then award the badge
Something like this
local Players = game:GetService("Players")
local part = script.Parent
local badgeService = game:GetService("BadgeService")
local badge = 2140926703
part.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then
return
end
local hasBadge = badgeService:UserHasBadgeAsync(player.UserId, badge)
if hasBadge then
print( player.Name .. " owns this badge!")
else
badgeService:AwardBadge(player.UserId, badge)
end
end)
I am maybe a little late, but I assume it’s because you are testing the game in Studio and not as a real game. I don’t think that an awarding of a badge is possible in Studio. To check that the function was still fired I would recommend using print, to make sure the function was successfully fired.