Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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


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

✓ Accepted answer

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)

2 comments

0

Hi Thank you. It works :)

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.