Why does my code works only sometimes?

Sometimes the background changes colors and sometimes it doesn’t…
I was coding a system where when The Player joins it checks if he has a badge (the badge is from my game) and if he has then: Background color is changed to green.
Please help :frowning:

local BadgeService = game:GetService("BadgeService")
local StarterGui = game:GetService("StarterGui")
local Floppa = StarterGui.Floppers2000.Frame.EasyFrame.Floppa
local BadgeId = 1956881106983885

local function CheckBadge(player, badgeId)
	local Success, badge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
	end)
	return badge
end


game.Players.PlayerAdded:Connect(function(player)
	local HasBadge = CheckBadge(player, BadgeId)
	if HasBadge then
		Floppa.BackgroundTransparency = 0.5
		Floppa.BackgroundColor3 = Color3.new(0.501961, 1, 0.447059)
	end
end)

StarterGui is a service where the UIs are stored in. When you start the game, all of the contents inside StarterGui are copied over to Player.PlayerGui. So when you change a UI inside StarterGui while the game is running, nothing will happen until the player dies, and the UIs are copied over to their PlayerGui again.

Note: An UI will only get copied once if the ResetOnSpawn property of a ScreenGui is set to false. When the player dies, if this property is set to false, the UI won’t get deleted, and thus a new one won’t be copied over to the PlayerGui.

local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer.PlayerGui
local Floppa = PlayerGui.Floppers2000.Frame.EasyFrame.Floppa

Update your code with these lines and it should work.

More information: StarterGui | Documentation - Roblox Creator Hub

1 Like

Dont use StarterGui, instead use PlayerGui

Player.PlayerGui

1 Like

Hello, Here is my take on fixing your issue and optimizing your script for you. If you have any questions feel free to ask!

--|< Services >|--
local BadgeService = game:GetService("BadgeService");
local Players = game:GetService("Players");

--|< Variables >|--
local localPlayer = Players.LocalPlayer;
local playerGui = localPlayer.PlayerGui;

local floppaFrame = playerGui:FindFirstChild('Floppers2000', true).Frame.EasyFrame.Floppa;
local badgeId = 000000000;

--|< Functions >|--
local function checkBadge(player, badgeId)
    local success, hasBadge = pcall(function();
        return BadgeService:UserHasBadgeAsync(player.UserId, badgeId);
    end)
    if success then
        return hasBadge;
    else
        warn("Error checking badge for player:", player.Name);
        return false;
    end
end

local function onPlayerAdded(player)
    local hasBadge = checkBadge(player, badgeId);
    if hasBadge then
        floppaFrame.BackgroundTransparency = 0.5;
        floppaFrame.BackgroundColor3 = Color3.new(0.501961, 1, 0.447059);
    end
end

--|< Connections >|--
Players.PlayerAdded:Connect(onPlayerAdded);
1 Like

Sorry, but your answer doesn’t solve the question, in fact, it does nothing more than to make the code look a little bit more pretty. And possibly confuses the author.

Dont use startergui.

I used to do that all the time and question why my code didnt work.

Instead get the player, then get .PlayerGui
For example:
local player = game.Players.LocalPlayer.PlayerGui

You are correct, I used StarterGui instead of PlayerGui. That is my bad and has been corrected.

1 Like

Hi i tried using the lines but it still only goes green when the Player dies, I was thinking about a solution which would be to reset the Player when he Joins so the Background can change,
But would that be lazy if i did?

No, but it could possibly add a weird glitch where its not green for a second.

Can you post the full code here with the lines I gave you?

Have you tried to use my solution?

here it is:

local BadgeService = game:GetService(“BadgeService”)
local Players = game:GetService(“Players”)
local PlayerGui = Players.LocalPlayer.PlayerGui
local Floppa = PlayerGui.Floppers2000.Frame.EasyFrame.Floppa
local BadgeId = 1956881106983885

local function CheckBadge(player, badgeId)
local Success, badge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
return badge
end

game.Players.PlayerAdded:Connect(function(player)
local HasBadge = CheckBadge(player, BadgeId)
if HasBadge then
Floppa.BackgroundTransparency = 0.5
Floppa.BackgroundColor3 = Color3.new(0.501961, 1, 0.447059)
end
end)

When i try your solution it still only works when the Player dies

Is this inside a LocalScript instance?

No it is inside “ServerScriptService” and is a Server script

Make it a LocalScript and put it inside StarterPlayerScripts.
And update your code to this:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer.PlayerGui
local Floppa = PlayerGui:WaitForChild("Floppers2000").Frame.EasyFrame.Floppa
local BadgeId = 1956881106983885

local function CheckBadge(player, badgeId)
    local Success, badge = pcall(function()
         return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
    end)
    return badge
end

local HasBadge = CheckBadge(player, BadgeId)
if HasBadge then
    Floppa.BackgroundTransparency = 0.5
    Floppa.BackgroundColor3 = Color3.new(0.501961, 1, 0.447059)
end
1 Like

Hello it worked :smiley:
Thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.