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
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.
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);
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.
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?
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)
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