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.
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.
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)
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)
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)