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

What am I doing wrong in this badge script? I can't seem to award the badge.

Asked by 4 years ago
Edited 4 years ago

I've tried multiple different scripts but none seem to work, I've looked at YouTube tutorials and even other questions here, this is my current code:

local badgeId = 2124494023 --Badge ID local awarded = true

script.Parent.Touched:connect(function(hit) local plr = game.Players:FindPlayerFromCharacter(hit.Parent) if awarded and plr then game.BadgeService:AwardBadge(plr.userId,badgeId)
awarded=false end end)

1 answer

Log in to vote
1
Answered by 4 years ago

The reason why you can't award the badge is that there's no method called FindPlayerFromCharacter in the Players service, you would have to use GetPlayerFromCharacter(). Also, your debounce logic is flipped but it doesn't really matter since it still serves the same purpose. Regardless, a better way to check if player already has a badge is using the UserHasBadgeAsync method.

Also, camelCase (variableName) connect is deprecated in favour of PascalCase Connect (VariableName). userId is also deprecated in favour of UserId.

local badgeId = 2124494023
local BadgeService = game:GetService("BadgeService")

script.Parent.Touched:Connect(function(hit)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if plr and not BadgeService:UserHasBadgeAsync(plr.UserId, badgeId) then
        BadgeService:AwardBadge(plr.UserId, badgeId)
    end
end)

If you have any questions please ask them in the comments, and if this answered your question, please mark it as the correct answer.

0
Can't thank you enough, that worked! SharkMelonWater 2 — 4y
Ad

Answer this question