I wanna award a badge when a number on a leaderstat is reached, however, I don’t know how to do so.
Edit: First script tracks distance, and when that distance is reached it fires a RemoteEvent that awards a badge.
Script 1:
local function CalculateDistance(character, distance)
local HRP = character:WaitForChild("HumanoidRootPart")
local Humanoid = character:WaitForChild("Humanoid")
local HasDied = false
Humanoid.Died:Connect(function() HasDied = true end)
local PreviousPosition = HRP.Position
repeat
task.wait(0.1)
local distanceTravelled = (PreviousPosition - HRP.Position).magnitude
if distanceTravelled > 1 then
distance.Value += distanceTravelled
PreviousPosition = HRP.Position
end
until HasDied
end
game.Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local distance = Instance.new("IntValue")
distance.Name = "Distance"
distance.Value = 0 + -100
distance.Parent = stats
local character = player.Character or player.CharacterAdded:Wait()
player.Changed:Connect(function(property)
if property == "Character" then
CalculateDistance(player.Character or player.CharacterAdded:Wait(), distance)
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = stats
local hight = leaderstats.distance
local RepSto = game:GetService("ReplicatedStorage")
local AwardBadge = RepSto.AwardBadge
if hight.Value >= 5 then
AwardBadge:FireServer()
end
end)
end
end)
CalculateDistance(character, distance)
end)
Script 2:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local AwardBadge = ReplicatedStorage.AwardBadge
local BadgeId = 2129523826
AwardBadge.OnServerEvent:Connect(function(player)
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(BadgeId)
end)
if success and badgeInfo.IsEnabled then
local success, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, BadgeId)
end)
end
end)
Now the leaderstat part of the script doesn’t work, when I changed it to a LocalScript. Is there any way I can make the badge award part of the script work with the leaderstat part without making seperate scripts? I always get confused when it comes to leaderstats.
local BadgeService = game:GetService("BadgeService")
function AwardABadge(plr, BadgeId: number)
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(BadgeId)
end)
if success then
if BadgeService:UserHasBadgeAsync(plr, BadgeId) then
warn("User Owns Badge")
else
print("Awarded")
BadgeService:AwardBadge(plr, BadgeId)
end
end
end
game.Players.PlayerAdded:Connect(function(p)
-- Your Leaderstats code
if Item.Value >= 5 then
AwardABadge(p.UserId, 2129523826)
end
end)
The return in the pcall function assigns the info from BadgeService:GetBadgeInfoAsync(BadgeId) to badgeInfo, in the AwardABadge function it returns the result of awarding the badge (which is a boolean value in this case).
Oftentimes, a standard return is used to make sure a function is stopping, however it can also be used to return specific values.
For example, if you had assigned a variable to the result of AwardABadge(p.UserId, 2129523826), it would either equal nil (when not success as not alternate response was coded) or the boolean value.
If you really want to use the LocalScript to avoid using the server checking everytime the value has changed, that is fine. However, you need to remember a key rule with programming:
Never trust the client.
When the remote fires (from the client to the server) make sure you then double-check that value on the server to make sure it should be awarded.