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

How to make this script give a badge to all players when value is true?

Asked by 3 years ago

local BadgeId = 1234 local ValueLocation = game.Workspace.Success.Value local ValueAmount = 1 print(script.Name .." Loaded.") while ValueLocation == ValueAmount do print("Value correct.") local BadgeService = game:GetService("BadgeService") game:GetService("Players").PlayerAdded:Connect(function(player) local function awardBadge(player, badgeId) if BadgeService:GetBadgeInfoAsync(BadgeId).IsEnabled == true then BadgeService:AwardBadge(player.UserId, badgeId) end end end) wait(5) end

Ive tried a lot of things and nothing has worked, i hope someone can help. Thanks.

1 answer

Log in to vote
0
Answered by 3 years ago

You have mentioned that you want to award all players a badge when the value is true. I assume that whenever the valueAmount becomes 1, it will award all players with the badge. In this case, you would use the .Changed Event.

Then in order to award all players the badge, you would loop through all players using the game.Players:GetPlayers() method.

So it should look something like this:

local BadgeService = game:GetService("BadgeService")
local BadgeId = 1234 --Change the BadgeID to the badge you want to award the player with.

local ValueLocation = game.Workspace.Success --Reference the IntValue obj, not the value itself.

local ValueAmount = 1

local function awardAllPlayers(newValue)
    if not newValue == ValueAmount then return end --If the value changes to a value that is not the ValueAmount, do nothing.

    --Award All Players
    --Loop Through All Players
    for _, player in pairs(game.Players:GetPlayers()) do
        --Wrap this around a pcall to prevent the script from breaking.
        local success, result = pcall(function()
            return BadgeService:GetBadgeInfoAsync(BadgeId)
        end)

        --If the badge information was successfully retreive them reward the players
        if success then
            if result.IsEnabled then
                BadgeService:AwardBadge(player.UserId, BadgeId)
            end
        end
    end
end

--Changed Event
ValueLocation.Changed:Connect(awardAllPlayers)
0
Hi Thank you. It works :) Nath390Fish 19 — 3y
0
No problem! ArsonMcFlames 363 — 3y
Ad

Answer this question