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

How to award a badge to the player through an item?

Asked by 6 years ago

So I have a script that is placed in a gear inside ReplicatedStorage, when the player has it in their inventory, they should receive a badge, however, it does not work. Help please.

if (script.Parent.Parent.Parent:FindFirstChild("Humanoid")) then
    print "lol"
p = script.Parent.Parent.Parent
    if (p ~= nil) then
        print("Awarding BadgeID: " ..script.Parent.BadgeID.Value .. " to UserID: " .. p.userId)
        local b = game:GetService("BadgeService")
        b:AwardBadge(p.userId, script.Parent.BadgeID.Value)
    end
end

Thanks.

1 answer

Log in to vote
0
Answered by
Pejorem 164
6 years ago
  1. Use better variable names such as "BadgeService" instead of "b"
  2. You don't need to compare something to nil because Lua conditionals have a value called "truthy". if it's true then it's true and truthy If it's false or nil then it's false if it's anything else it's truthy
  3. Award the badge consecutively with the system that gives them the gear in the first place. (4. Player.userId is deprecated use Player.UserId)
local marketplaceService = game:GetService("MarketplaceService")
local badgeService = game:GetService("BadgeService")

--code that gives tool here
if not marketplaceService:PlayerOwnsAsset(plr, script.Parent.BadgeID.Value) then
    badgeService:AwardBadge(plr.UserId, script.Parent.BadgeID.Value)
end
Ad

Answer this question