Script that check the leaderstat value of a player and awards badge when enough

What do you want to achieve?
I want to make a script that check the leaderstat value of a player until it reaches a certain value. When it reaches it, it must give a badge to the player.

What is the issue?
I have idea on how it would look but I don’t know how it would be in a script.

What solutions have you tried so far?
I searched on the DevForum/Youtube to find something but didn’t find.

Thanks for reading and maybe helping,

~ TheDeadpool2496.

1 Like

Can you paste the script that adds the leaderstats values?

1 Like

You can do a Changed function or a GetPropertyChangedSignal on the leaderstats value that you wish to monitor. You could then use an if statement to check if the value is high enough and if so, award the badge.

1 Like

Sure! It comes from a free model.

 the entire script, it’s a time played saving leaderstat (it comes from a free model):

--[[
         script made by Rodemallgames
    Put this script in ServerScriptService or it won't work

--]]


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

	local timeingame = Instance.new("IntValue")
	timeingame.Name = "Time"
	timeingame.Parent = leaderstats
	while true do
		wait(1)
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 1
	end
end)

-----------------------------------------------| Save Stats |------------------------------------------------------------


local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


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

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	local playerUserId = "Player"..player.UserId

	local data 

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId) 
	end)


	if success then
		Time.Value = data  
	end

	while wait(1) do
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 0 --Don't change it or it will make it go up twice
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player"..player.UserId

	local data = player.leaderstats.Time.Value

	myDataStore:SetAsync(playerUserId, data)   

end)

I know that but as I said I don’t know how to make that into a script…

Oh I see, sorry. Well after you create your leaderstats values. You can put in a function such as:

timeingame:GetPropertyChangedSignal("Value"):Connect(function()
if timeingame.Value >= minimumValue then
--award badge
end
end)
1 Like

You can use :AwardBadge to award the badge.

Try this:

local DataStoreService = game:GetService("DataStoreService")
local BadgeService = game:GetService("BadgeService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function AwardBadge(player, badgeId)
	local success, errorMessage = pcall(BadgeService.AwardBadge, BadgeService, player.UserId, badgeId)
	
	if not success then
		warn(errorMessage)
	end
end

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

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	local playerUserId = "Player"..player.UserId

	local data 

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId) 
	end)


	if success then
		Time.Value = data  
	end
	
	Time.Changed:Connect(function(newValue)
		if newValue > 1000 then
			AwardBadge(player, 10000 --[[Insert your badge id]])
		end
	end)
	
	while task.wait(1) do
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 0 --Don't change it or it will make it go up twice
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player"..player.UserId

	local data = player.leaderstats.Time.Value

	myDataStore:SetAsync(playerUserId, data)   

end)
3 Likes

Create another Server script in ServerScriptService.
Then use the following code example below:

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local BadgeId = -- put your Badge ID here

Players.PlayerAdded:Connect(function(player)
	local timeValue = player:WaitForChild("leaderstats"):WaitForChild("Time") -- gets the 'Time' Value
	
	timeValue.Changed:Connect(function() -- detects if the 'Time' value changes
		if timeValue.Value >= 100 then -- change '100' to the number the player has to reach to earn the badge
			BadgeService:AwardBadge(player.UserId, BadgeId) -- awards the badge
		end
	end)
end)
2 Likes

I see this is exactly the same thing as in the free model and I don’t understand what it does in this script?

@Codeularity @BabyNinjaTime thanks, I will try that.

I tried that but it didn’t work… My value was at “90,502” and I have set the value required in the script at “90100”…

I tried your script and it didn’t work… I have set the value as “90600” and waited until my value reach it and I didn’t get the badge.

If one of these conditions do not meet, then that would likely be the reason why the script isn’t awarding the badge.

1 Like

No, there’s no issues with that.

When you tried Katris’ script, did it return an error message or did it simply do nothing at all?

1 Like

Oh I’m dumb I totally forgot to check that… Sorry, I’ll check that at soon as possible!

Did you set the badge id correctly?