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

Award badge based off of leaderboard stats?

I want to make it where if the timer on the leaderboard goes to 60 minutes, then you get a 1 hour badge. I'm not sure how to do this though. Here's a script I half made that doesn't work


game.Players.PlayerAdded:connect(function(player)
 local Stats = Instance.new("Model", player)
 Stats.Name = "leaderstats" 
 local Timer = Instance.new("IntValue", Stats)
 Timer.Name = "Time Wasted"
 Timer.Value = 0
 while true do 
  wait(60)
  Timer.Value = Timer.Value +1
if player.Stats.Timer.Value>=60 then -- if they are at 60 minutes it awards the badge
	AwardBadge(player.userId,badgeId)
end
 end
end)

local badgeId=2100422700
local badgeService=game:GetService("BadgeService")
function AwardBadge (userId,badgeId)
	if not badgeService:UserHasBadge(userId,badgeId)then
		badgeService:AwardBadge(userId,badgeId)
		
	end
end

2 comments

0

The parent argument to Instance.new is deprecated, assign the parent in another line. Player.userId is deprecated, switch to Player.UserId. :connect is deprecated, switch to :Connect.

0

where is the parent argument to instance.new?

1 answer

✓ Accepted answer

You have an infinate loop in the player added event which will stack over time causing your game to break.

In your use badgeId on line 11 but that variable does not exist until line 16 which is probably what is causing your script not to work.

There are a lot of ways you can check for when the value is above 60 but I would just connect a event to that timer value object and then disconnect it after this will mean you would only run that check for players under 60 minutes.

Example:-

-- services
local plrServ = game:GetService("Players")
local badgeService = game:GetService("BadgeService")

-- var
local badgeId = 2100422700

plrServ.PlayerAdded:connect(function(plr)
	local Stats = Instance.new("Model")
	Stats.Name = "leaderstats"
	local Timer = Instance.new("IntValue")
	Timer.Name = "Time Wasted"
	Timer.Parent = Stats 
	Stats.Parent = plr

	-- add the event only for player who do not own the badge
	if not badgeService:UserHasBadge(plr.UserId, badgeId ) then
		local con -- needs to be defined first
		con =Timer.Changed:Connect(function(newVal)
			if newVal == 60 then
				 badgeService:AwardBadge(plr.UserId, badgeId)
				con:Disconnect() -- disconnect this event
			end
		end)
	end
end)

-- add time for all players
-- not this will mean they share the same clock
while true do
	wait(60)
	for _, plr in pairs(plrServ:GetPlayers()) do
		
		-- find stats
		local leaderStats = plr:FindFirstChild("leaderstats")
		if leaderStats then 
			local timeVal = leaderStats:FindFirstChild("Time Wasted")
			if timeVal then
				timeVal.Value = timeVal.Value + 1
			end
		end
	end
end

I hope this helpe please comment if you do not understand how / why this code works

9 comments

0

UserHasBadge was broken and is deprecated. It's UserHasBadgeAsync

0

How would I add more badges to this script?

0

@incapaz They really need to fix the wiki so much incorrect info now :/

0

@retracee you would use some form of table but it will depend on what badges are used for what. You would be better off in this case to make a module to handle badge requests then just require the module.

0

im not sure what that means:( but could I copy the if not statement down to the con disconnect and the two ends and paste it a second time in the script with a new badge id?

0

you could do test that out

0

i tried doing that, there are no red lines but it still wont work :(

0

Post another question with your code you are using.

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