So basically I’m trying to make a gui where if someone owns a badge, a imagebutton will appear and they can press it to teleport. But if they don’t own the badge, then the imagebutton will not be visible and they cannot click it.
It currently does not work but there’s no errors in the output. Can someone help me? This is my code:
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 2046730559462624
local button = script.Parent
local function onPlayerAdded(player)
if BadgeService:UserHasBadgeAsync(badgeId) then
button.Visible = true
else
button.Visible = false
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeId = 00000000 -- Change this to your badge ID
local function onPlayerAdded(player)
-- Check if the player has the badge
local success, hasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has badge!")
return
end
if hasBadge then
-- Handle player's badge ownership as needed
print(player.Name, "has badge", badgeId)
end
end
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
Local Scripts Are stupid, basically. PlayerAdded doesn’t fire really first when the client joins in. You can just get the player like this:
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local badgeId = 2046730559462624
local button = script.Parent
if BadgeService:UserHasBadgeAsync(player.UserId, badgeId) then
button.Visible = true
else
button.Visible = false
end