Join script that detects badge or gamepass wont work!

I am making a script that detects if the player has a badge or a gamepass. Once the player joins it should detect if the player has a badge or a gamepass and make the door nvisible (just to see if it works)

I found no answers online so I would be very happy if you could help.

door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")


game.Players.PlayerAdded:connect(function(player)
	if player.Parent:FindFirstChild("Humanoid") then
		local p = game.Players:GetPlayerFromCharacter(player.Parent)
		if p then
			if badgeservice:UserHasBadgeAsync(p.UserId, badgeholder) then

			elseif badgeservice:UserHasBadgeAsync(p.UserId, badgeholder) == false then
				if marketplace:UserOwnsGamePassAsync(p.UserId, gamepassholder) == false then
				door.Transparency = 0
				end
			end
		end
	end
end)
1 Like

oh my theres alot of problems here that im nitpicky about.

First off when doing a Playeradded event, The event returns the player object in game.Players already, so all you need to do is

game.Players.PlayerAdded:Connect(function(plr)
local Char = plr.Character
end)

Next if you want to make a door invisible for a certain player, your gonna need to communicate this to that players client via remote events
Or if you want that player to go through
door, use collision groups

For checking if a user owns a badge, wrap it in a pcall like so
local succ, result = pcall(function()
return BadgeService:UserHasBadge(plr.Userid, BadgeId)
end)

Im on mobile so its a little weird

Hope i helped you, Let me know if you need any more help/advice. :slight_smile:

1 Like

I tried that and it give me this error
Screenshot 2024-12-27 004752

1 Like

Sorry i apologize im on mobile so its

Players.PlayerAdded

Mb

1 Like

I got this script

door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")


game.Players.PlayerAdded:Connect(function(plr)
	local Char = plr.Character
		local player = game.Players:GetPlayerFromCharacter(Char)
		if player then
			if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then

			elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
				if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
				door.Transparency = 0
			end
		end
	end
end)

It does not make the door part visible without any errors
(and yes I made the value to a random number so I dont have badge or gamepass)

oh my brother

here this is in the PlayerAdded function:
PlayerAdded RETURNS THE PLAYER OBJECT ALREADY FROM GAME.PLAYERS!

This code is pasted in the Playeradded function


local Char = plr.Character -- THIS IS THE CHARACTER 
local succ, err = pcall(function() -- THIS CATCHES ERRORS
       return BadgeService:UserOwnsBadgeAsync(plr.Userid, BadgeId)
end)


if succ
      print("user owns badge")
end

i

door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")


game.Players.PlayerAdded:Connect(function(plr)
	local Char = plr.Character
		local player = game.Players:GetPlayerFromCharacter(Char)
		if player then
			if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then

			elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
				if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
				door.Transparency = 0
				end
			local Char = plr.Character -- THIS IS THE CHARACTER 
			local succ, err = pcall(function() -- THIS CATCHES ERRORS
				return badgeservice:UserOwnsBadgeAsync(plr.Userid, badgeholder)
			end)


			if succ then
			print("user owns badge")
		end	
		end
	end
end)

No like this,

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")

Players.PlayerAdded:Connect(function(player)
        --player returns playerobject in game.Players
local Character = player.Character

  local HasBadge, Result = pcall(function()
        return BadgeService:HasBadge(player.Userid, badgeid)
end)

  if HasBadge then
     print("Player has badge, make door visible or transparent!")
    end

end)

???

door = script.Parent
badgeholder = door.Parent.BadgeID.Value
gamepassholder = door.Parent.GamepassID.Value
local badgeservice = game:GetService("BadgeService")
local marketplace = game:GetService("MarketplaceService")
local Players = game:GetService("Players")


game.Players.PlayerAdded:Connect(function(plr)
	local Char = plr.Character
		local player = game.Players:GetPlayerFromCharacter(Char)
		if player then
			if badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) then

			elseif badgeservice:UserHasBadgeAsync(player.UserId, badgeholder) == false then
				if marketplace:UserOwnsGamePassAsync(player.UserId, gamepassholder) == false then
				door.Transparency = 0
			end	
		end
		end
	local Character = player.Character

	local HasBadge, Result = pcall(function()
		return badgeservice:HasBadge(player.Userid, badgeholder)
	end)

	if HasBadge then
		print("Player has badge, make door visible or transparent!")
	end

end)

delete the script and add my changes, why is this so complicated?

there i did it :smiley:

Screenshot 2024-12-27 011343

also i did something wrong inside the pcall
its: return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)

Screenshot 2024-12-27 011646

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeid = script.Parent.Parent:WaitForChild("BadgeID")

Players.PlayerAdded:Connect(function(player)
	--player returns playerobject in game.Players
	local Character = player.Character

	local HasBadge, Result = pcall(function()
		return BadgeService:UserHaaBadgeAsync(player.UserId, badgeid)
	end)

	if HasBadge then
		print("Player has badge, make door visible or transparent!")
	end

end)

I spelt has wrong its
UserHasBadgeAsync NOT userHaaBadgeAsync

Screenshot 2024-12-27 011931

can i see the code pleaseeeeeeee

local Players = game:GetService("Players")
local BadgeService = game:GetService("BadgeService")
local badgeid = script.Parent.Parent:WaitForChild("BadgeID")

Players.PlayerAdded:Connect(function(player)
	--player returns playerobject in game.Players
	local Character = player.Character

	local HasBadge, Result = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeid)
	end)

	if HasBadge then
		print("Player has badge, make door visible or transparent!")
	end

end)

this might mean you dont own the badge , check if that badge # exists

It’s suppose to detect if the player doesnt have the badge and also the gamepass