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



--EGGDOG CHECKER--

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

local players = game.Players:GetPlayers()

for _, player in pairs(players) do
	local userId = player.UserId
	local x = true

	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)
			x = not x
		end
	else
		print("Error checking badge for " .. player.Name)
	end

	function onTouch(hit)

		if x == false then
			local h = hit.Parent:FindFirstChild("Humanoid")
			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
	end

	script.Parent.Touched:connect(onTouch)
end

Its quite confusing how you want it, for me Could have just put the checking badge inside the touch function and not use for i loop. and get the player from character from the touched hit parameters, local playerFromCharacter = playerService:GetPlayerFromCharacter(character).

function onTouch(hit)
                local BADGE_ID_EGGDOG = script.Parent.BadgeID
                 local plyr = playerService:GetPlayerFromCharacter(hit.Parent)
	local success, hasBadge = pcall(function()
		return b:UserHasBadgeAsync(plyr.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 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)

playerservice shows up as red line under it

add this alway at the top AND put my new editted script in:

local playservice = Game:GetService("Players")

It doesn’t work, 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