Badge Given to player after x amount of time

Ight so it’s probably really simple but after a few hours of testing myself and looking around on youtube and here, I came across no help. So I’m posting here to maybe see if someone can help because Im honestly gonna give up lol.

It’s quite simple, I just want a player to earn a badge for a certain amount of time spent in server. Example, you spend an hour in game and you are rewarded with a badge. I have a leaderboard system with Days, Hours, Minutes and Seconds (in that order. I have a DataSave and the time does work correctly).

The Issue:

Below is the script for the 1 Hour badge award for being in game; the issue is that the Server Script storage doesn’t recognize the Players.LocalPlayer or anything in that nature. The direct error code is this " leaderstats is not a valid member of RBXScriptSignal" BUT I’ve also come across this error code too "ServerScriptService.TimeplayedBadges.1 Hours:3: attempt to index nil with ‘leaderstats’ "

local badgeService = game:GetService(“BadgeService”)
local badgeID = (Removed for privacy, actual badge ID is placed here without the brackets.)
local leaderstats = script.Parent.Parent.Parent.Players.LocalPlayer.leaderstats

game.Players.PlayerAdded:Connect(function(plr)
while wait(0.05) do
if leaderstats.Hours.Value >= 1 then
if not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
badgeService:AwardBadge(plr.UserId, badgeID)
print(“1 Hour Given”)
end
end
end
end)

I have tried a whole array of things; from doing something like
local leaderstats = game:GetSertice(“Players”).LocalPlayer.leaderstats.Hours.Value
to what we have here which is a unnecessary amount of Parents.

I’m not a pro scripter; I’m just getting the hang of it but this is honestly stumping me.

If you need any more info do not hesitate to ask!!!

4 Likes

local badgeService = game:GetService(“BadgeService”)
local badgeID = (ID here)
local leaderstats = script.Parent.Parent.Parent.Players.LocalPlayer.leaderstats

game.Players.PlayerAdded:Connect(function(plr)
while true do()
wait(0.1)
if leaderstats.Hours.Value >= 1 and not badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
badgeService:AwardBadge(plr.UserId, badgeID)
print(“1 Hour Given”)
end
end
end)

As you said, server can’t see client and LocalPlayer is a client thing. So the best way as AviaBasil wrote up there and the method that people use to see a player on the server is
game.Players.PlayerAdded:Connect(function(Player)
The Player is much like LocalPlayer so everyone tends to use that. I just decided to write this because he just dumped the code and didn’t explain what you’re doing wrong.

I’m grateful for your response, I really am! :smiley: but this error code came up. It has something to do with line 3 (local leaderstats = script.parent.parent.parent.players.LocalPlayer.leaderstats)

  • ServerScriptService.TimeplayedBadges.1 Hour:3: attempt to index nil with ‘leaderstats’ *

Thank you for explaining :smiley: I do appreciate it a ton! I can understand what you mean but something to do with line 3 is what is giving me the error :frowning:

Remove the entire line, you won’t use it because it still has LocalPlayer in it. Instead do plr.leaderstats.

local badgeService = game:GetService(“BadgeService”)
local badgeID = (ID here)

game.Players.PlayerAdded:Connect(function(plr)
   while true do()
      wait(0.1)
      if plr.leaderstats.Hours.Value >= 1 and not 
         badgeService:UserHasBadgeAsync(plr.UserId, badgeID) then
         badgeService:AwardBadge(plr.UserId, badgeID)
         print(“1 Hour Given”)
     end
   end
end)
8 Likes

Oh I see what you mean now!
and yes, It works! Omg Thank you both!

1 Like