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?
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)