Help with teleport gui using UserHasBadgeAsync()

Hey developers,

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)

:heart: Thanks!

Have you changed the variable “badgeId” to your badge ID?

1 Like
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)

I just did but it still doesn’t work, I’ll update the topic

Tried it but it does not work and you used assistant to make that.

Then I suggest you put some print statements.
What type of script are you using and where did you put it (Service)?

check if there is another script that uses badgeservice and if the script is interfering with your script

Will do.

Local script, In startergui because I think it’s easier but I don’t know.

Idk if that would matter or not but ok…

Bruh I notice the error, you forgot to put the UserId as a variable in the Method

2 Likes

I added a print to check if anything happened and nothing printed in the output so I’ve done something wrong…

Paste this:

if BadgeService:UserHasBadgeAsync(player.UserId, badgeId) then
1 Like

Tried that and still doesn’t work and nothing printed again

try to put the script on serverscriptservice

Why would I do that? It’s a script that’s ment to be controlled by the client.

Ok I know now the issue:

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
1 Like

Thank you so much that fixed it!

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