Area for users with a certain badge

I was trying to make a wall that players can only get through if they have a badge from a previous section of the game. I tried to search up how to make it but the information I found wasn’t really helpful, plus I couldn’t understand it since I’m new to Roblox scripting. Can somebody help?

This was the code I got from the site

local BadgeService = game:GetService(“BadgeService”)
local Players = game:GetService(“Players”)

local badgeID = 2124574274 – Change this to your badge ID

local function onPlayerAdded(player,PlayerAdded)
– 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
	game.Workspace.MediumDoor.CanCollide = false
end

end

– Connect “PlayerAdded” events to the “onPlayerAdded()” function
Players.PlayerAdded:Connect(onPlayerAdded)

Also sorry if I have made a dumb mistake, I’m still new to scripting on Roblox so I don’t understand how to do this!
Also where should the script be and which kind?

Thank you!

3 Likes

I see the mistake that happened here. It isn’t BadgeService:UserHasBadgeAsync(), but it is BadgeService:UserHasBadge()

2 Likes

I recomand using a local script for that.
When someone joins and has the badge everyone else could go trought the door.

3 Likes

In fact “UserHasBadgeAsync()” is the new variant.
https://developer.roblox.com/en-us/api-reference/function/BadgeService/UserHasBadgeAsync

2 Likes

Oh! Well I didn’t know that. I never have used it before so yeah

1 Like

Put it on a LocalScript on “ReplicatedFirst” Service.

local badgeId = 2124574274 
local bs = game:GetService("BadgeService")

local part = workspace:WaitForChild("MediumDoor")
local plr = game.Players.LocalPlayer


local function detect(player)
	pcall(function()
		if bs:UserHasBadgeAsync(player.UserId,badgeId) then
			return true
		else
			return false
		end
	end)
end


part.Touched:Connect(function(hit)
	if hit.Parent.Name == plr.Name then
		local p = game:GetService("Players").GetPlayerFromCharacter(hit.Parent)
		if detect(p) then
			part.CanCollide = false
		end
	end
end)

2 Likes

You could also return just the badge function it returns true or false on his own :slightly_smiling_face: for the rest it looks good

1 Like

I tried to use this code but players who don’t have the badge can still go through the door unless I’ve missed out something but I don’t think I did

You can also work with collision groups that can pass through a door maybe that works better just add a collision group and on character added you can set the collision group to the player. I cant type out a script due being on phone but here is more information about it
https://developer.roblox.com/en-us/articles/Collision-Filtering
https://developer.roblox.com/en-us/api-reference/class/PhysicsService

1 Like