Leaderstats badge not working? Help

Hey my script isn’t working! What did i do wrong?

Code:

function onPlayerEntered(newPlayer)

   local stats = Instance.new("IntValue")
   stats.Name = "leaderstats"

   local mins = Instance.new("IntValue")
   mins.Name = "Dips"
   mins.Value = 0


   mins.Parent = stats
   
   stats.Parent = newPlayer
if newPlayer.Name == "EggService" or "BusinessUwU" then
   mins.Value = 9999
end


   while wait(1) do
   if newPlayer.leaderstats.Dips.Value == 10000 then
   	local BadgeService = game:GetService("BadgeService")
   
   local function awardBadge(player, badgeId)
   	-- check badge can be awarded
   	if BadgeService:IsLegal(2124484177) and not BadgeService:IsDisabled(2124484177) then
   		-- award badge
   		BadgeService:AwardBadge(newPlayer.UserId, 2124484177)
   	end
   end
   end
   end
end



game.Players.ChildAdded:connect(onPlayerEntered)

Is there anything wrong? Please let me know how to fix this.

3 Likes

its supposted to give a badge at 10000 dips.

For starters, you didn’t call the function.

You should also replace the newPlayer variable and the numbers with the arguements within the function (replace “newPlayer” with player, and the id of the badge with "badgeId’

Also, you’re not using the PlayerAdded event. You’re using the ChildAdded event for players, which shouldn’t be used in this situation.

but where to put the badgeId then? i dont see a variable

The function “awardBadge” defines the variables player and badgeId. You have to call the function, which you didn’t do. Call it like this: awardBadge(newPlayer, 2124484177).
The point of a function is to be interchangeable, so you can call a snippet of code without having to rewrite it over and over again. Here’s what you do with the awardBadge function:

local function awardBadge(player, badgeId) -- these are the arguements/variables
   	-- check badge can be awarded
	if BadgeService:IsLegal(badgeId) and not BadgeService:IsDisabled(badgeId) then -- replace these two
		-- award badge
		BadgeService:AwardBadge(player.UserId, badgeId)  -- replace here
	end
end

You should it inside the while wait(1) do loop.

still doesnt work. i dont know whats wrong

Wait, to clarify did you call the function?

Please don’t use the Scripting Support category as a dump-and-run hub for broken code. Posting your entire code and saying it’s broken is not only improper use of the category but an inconvenience for those who are replying to your question. Please see the category guidelines for more information.

In spite of the above information, I’m going to help you out with your code. There’s only one major issue and that has to do with the fact that your loop is blocking the rest of the script from running.

Here are some bones to pick with your code:

  • There’s a lot of unnecessary whitespace. Get rid of those to help with the readability of your code.

  • Use clear variable names. Your stat value variable name is mins yet the name of the actual data is Dips. Seeing as your Dips aren’t incremented every minute, this is useless to include.

  • In the case of working with features for certain users, you should work with their UserIds as opposed to their usernames. Usernames are change-bound.

    • If you’re too lazy to fish out the UserId of a user, you can pass a username through Players.GetUserIdFromNameAsync. It will return the UserId associated with the username, regardless of whether or not it’s a player’s current username.
  • Don’t use a while loop here.

    • In terms of practice, while wait loops are not good. You also should not be having that loop inside of your PlayerAdded handler.

    • You shouldn’t be using a loop here at all. What you should be doing is hooking a function that detects when the value changes and award it from there.

  • Hold things in variables so you don’t have to repeat them. In your badge function, you pass the id three times. Instead, you can assign a variable to the id and pass the variable three times. This is just something for sanity and convention’s sake.

  • The badge function isn’t ever called, so it’ll never be awarded. You don’t need to check the legality or disabled properties of the badge either, realistically. Wrap the badge call in a pcall to catch errors received from attempting to award the badge.

Here is some code you can work with. You’ve already got code so I’m just rearranging it and applying new fixes; normally, I wouldn’t give you the code if you didn’t already have a proto down.

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

local BADGE_ID = 2124484177

-- player is easier to write out, so I changed the variable name
local function playerEntered(player)

    -- Use a Folder, more appropriate
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"

    local dips = Instance.new("IntValue")
    dips.Name = "Dips" -- New IntValues have a default value of 0

    -- Example of what I meant by use Players.GetUserIdFromNameAsync
    if player.UserId == Players:GetUserIdFromNameAsync("EggService") or player.UserId == Players:GetUserIdFromNameAsync("BusinessUwU") then
        dips.Value = 9999
    end

    dips.Changed:Connect(function (value)
        -- In case Dips goes over 10000, give those players the badge too
        if value >= 10000 then
            -- Wrap the function call directly
            pcall(BadgeService.AwardBadge, BadgeService, player.UserId, BADGE_ID)
        end
    end)

    dips.Parent = stats
    stats.Parent = player
end

Players.PlayerAdded:Connect(playerEntered)

for _, player in pairs(Players:GetPlayers()) do
    playerEntered(player)
end

@mobyboyy

The only valid fix in your post is that the function wasn’t called. Nothing else needs to be replaced. PlayerAdded is simply the canonical function to use here; it’s a version of ChildAdded that only fires for players and follows different protocols.

No matter the players name, this if statement will run its contents. The case you have given the if statement will check if the players name is equal to EggService, or if it’s not it will check the second argument to the “or” keyword - which in your case is a valid string, resulting as true.