Script to generate a gui if user does not have badge

Im having a few issues with copying a gui into playergui. and making it visible. I tried adding a badge checker but it doesn’t work at all

i want it like to check if u have a badge, if you have it the gui wont appear, but if you don’t the gui will appear

local Popup = script.Parent.ScreenGui
local Ready = true
local b = game:GetService("BadgeService")


local player = game.Players:GetPlayers()
local userId = player.UserId
local playerService = game:GetService("PlayerService")
local x = true



function onTouch(hit)
	local BADGE_ID_EGGDOG = script.Parent.BadgeID

	local success, hasBadge = pcall(function()
		return b:UserHasBadgeAsync(userId, BADGE_ID_EGGDOG)
	end)

	if success then
		if hasBadge then
			print(player.Name .. " has badge " .. BADGE_ID_EGGDOG)
			x = nil
		else
			print(player.Name .. " does not have badge " .. BADGE_ID_EGGDOG)
			local h = hit.Parent:FindFirstChild("Humanoid")
			local plyr = playerService:GetPlayerFromCharacter(hit.Parent)
			if h ~= nil and Ready == true then
				Ready = false
				local plyr = game.Players:FindFirstChild(h.Parent.Name) 
				local c = Popup:clone()
				c.Parent = plyr.PlayerGui
				wait(5) 
				c:remove() 
				Ready = true
				script:Destroy()
			end
		end
	else
		print("Error checking badge for " .. player.Name)
	end

end

script.Parent.Touched:connect(onTouch)
3 Likes

GetPlayers() returns a table, userId is not an object in this table

What you’re looking for is a specific player, not the entire list of players in the game.

local Popup = script.Parent.ScreenGui
local Ready = true
local b = game:GetService("BadgeService")
local x = true



function onTouch(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if not Player then return end

	local BADGE_ID_EGGDOG = script.Parent.BadgeID

	local success, hasBadge = pcall(function()
		return b:UserHasBadgeAsync(Player.UserId, BADGE_ID_EGGDOG)
	end)

	if success then
		if hasBadge then
			print(Player.Name .. " has badge " .. BADGE_ID_EGGDOG)
			x = nil
		else
			print(Player.Name .. " does not have badge " .. BADGE_ID_EGGDOG)
			local h = hit.Parent:FindFirstChild("Humanoid")
			if h ~= nil and Ready == true then
				Ready = false
				local c = Popup:clone()
				c.Parent = Player.PlayerGui
				wait(5) 
				c:remove() 
				Ready = true
				script:Destroy()
			end
		end
	else
		print("Error checking badge for " .. Player.Name)
	end

end

script.Parent.Touched:connect(onTouch)

1 Like

doesn’t work??

The gui still didn’t appear

	local BADGE_ID_EGGDOG = script.Parent.BadgeID

this is probably erroring and is most likely an intvalue, try this

	local BADGE_ID_EGGDOG = script.Parent.BadgeID.Value

It worked! Thanks!

I think that was the original problem

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