Badge Ui not working

  1. What do you want to achieve? im making a little ui to display badges in my game, im trying to make it so if a player has a badge, the background color of the correspondant badge ui changes to a lighter shade.

screenshots:
default
image_2021-08-05_103400
obtained one badge
image_2021-08-05_103329

  1. What is the issue? my current code checks if the player has the badge, and if they do it changes the colour, if they dont it doesnt change anything.

but for some reason it doesnt work and i dont know why. theres no errors in Output, no nothing.

  1. What solutions have you thought of so far? ive searched on google and looked at devforums but havent found a solution

here is the code:

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local Badge1 = 2124751445
local GotBadgecolor = Color3.new(0.752941, 0.815686, 0.862745)
Players.PlayerAdded:Connect(function(player)
    if BadgeService:UserHasBadgeAsync(player.UserId, Badge1) then
        Badge1Ui.BackgroundColor3 = GotBadgecolor
        print("player has badge1!")
        --obtained
    else
        print("player doesnt have badge1!")
        --not obtained
  end
end)

im genuinely confused cause the same color changing worked on another script
also sorry if i posted on the wrong topic i am new,

I’m assuming this is a server script, then, you should use a remote event that fires the client with an order.

Server script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local Badge1 = 2124751445
Players.PlayerAdded:Connect(function(player)
    if BadgeService:UserHasBadgeAsync(player.UserId, Badge1) then
        print("player has badge1!")
        ReplicatedStorage.RemoteEvent:FireClient()
        --obtained
    else
        print("player doesnt have badge1!")
        --not obtained
  end
end)

Local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Badge1Ui = --insert ui variable here--
local GotBadgecolor = Color3.new(0.752941, 0.815686, 0.862745)

ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
   Badge1Ui.BackgroundColor3 = GotBadgecolor
end)

(disclaimer: untested code)

it is currently a localscript inside of the gui, but i will try out your code and see if it works !1

Yeah I’m pretty sure the badge service doesn’t work in local scripts, that might be the issue.

1 Like

thank you man! it worked! but i also edited some things with the Scripts and now its like this:
ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local Badge1 = 2124751445
Players.PlayerAdded:Connect(function(player)
    if BadgeService:UserHasBadgeAsync(player.UserId, Badge1) then
        local GotBadgeColor = Color3.new(--colorvalue--)
        print("player has badge1!")
        ReplicatedStorage.RemoteEvent:FireClient(player, GotBadgeColor)
        --obtained
    else
        print("player doesnt have badge1!")
        --not obtained
  end
end)```
LocalScript

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local Badge1Ui = --insert ui variable here–

ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(GotBadgeColor)
Badge1Ui.BackgroundColor3 = GotBadgecolor
end)

basically just switched over the ui color to the serverscript and made it go through the remoteevent cause it didnt work for me otherwise
2 Likes