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

How do I award badges related to leaderstats?

I know that I could user PlayerAdded event, but this is incompetent because I need the player to rejoin each and every time they hit an event to get the badge. Here is my script, what should I do so they get the badge as soon as they hit the event?

local BadgeService  = game:GetService("BadgeService")
local FirstKillsBadge = 2124615333

game.Players.PlayerAdded:Connect(function(player)
	local stats = game.Players[player.Name].leaderstats.Kills
	if stats.Value>=100 then
	BadgeService:AwardBadge(player.UserID, FirstKillsBadge)	
end)

This leads me back to a question I have asked but never has it been answered thoroughly. I want to know how I can access a folder inside a player without having to use the "PlayerAdded" event. I can't get badges to be awarded or text linked to leaderstats to be changed because I always use the "PlayerAdded" event to reference leaderstats.

1 answer

✓ Accepted answer

You should probably keep track of the player's kills in the same script that you insert the leaderstats instance into the player.

local BadgeService = game:GetService("BadgeService")
local FirstKillsBadge = 2124615333

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = stats

	stats.Parent = player

	kills:GetPropertyChangedSignal("Value"):Connect(function()
		if (kills.Value == 100) then
			BadgeService:AwardBadge(player.UserId, FirstKillsBadge)
		end
	end)
end)

Whenever a player joins, they will be given a leaderstats folder with a Kills IntValue inside of it.

After they're given these two things, a GetPropertyChangedSignal event will be connected with the value of the Kills IntValue, such that whenever it changes, we do something.

That something that we do is check to see if it's 100 kills. If it is, award it to the player. That event listener will run every time that player's Kills IntValue changes its value, until they leave the game.

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