Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make it so only a specific player can go through a barrier?

So this is my code (it is located in ServerScriptService as a regular script):

local players = game:GetService("Players")
local friendDoor = game.Workspace.friendDoor

local users = {
	-- this is where i put the id, but I don't want to reveal hers
}

local isAllowed = function(player, tab)
	for i, v in pairs(tab) do
		if players.LocalPlayer.UserId == v then return true end
	end
	return false
end

friendDoor.Touched:Connect(function(player)
		if isAllowed(player, users) then
		    friendDoor.CanCollide = false
		    wait(1)
		    friendDoor.CanCollide = true
       end
end)

When I touch the door/barrier, it gives me this error: 15:47:19.625 - ServerScriptService.OnlyFriendScript:10: attempt to index nil with 'UserId'

Which might mean that I can't get the Id with LocalPlayer?

Just to make sure everyone understands, the objective of this script is to only let my friend through the barrier and no other player on roblox. (Which means that even if I touch the barrier I can't get in, so that is what I am expecting to happen when I test.) Thanks

1 comment

0

make sure a player is touches the door

2 answers

LocalPlayer is only on the client and can only be seen from a local script. The server regulates the entirety of the game server.

Your system is also flawed because the door could be open then a non-admin can easily get in the "barrier" with the help of an authorized individual. To fix this, use Collision Filtering which utilizes the PhysicsService.

You can instead just mark an object as the barrier and if the player is authorized, add them to the collision group related to the barrier.

Below is a short server script I made that could work for you. Keep in mind that anything new added to the character like a tool or object will need to be set into the collision group for it to not collide as well since its part of the character and we only initially set the collision group for character's descendants when they spawn in. You can easily do this with ChildAdded or DescendantAdded.

local PhysicsService = game:GetService("PhysicsService")
 
local adminsGroup = "Admins"
local barriersGroup = "Barriers"

local barriers = {} -- put barriers here
 
-- Create two collision groups
PhysicsService:CreateCollisionGroup(adminsGroup)
PhysicsService:CreateCollisionGroup(barriersName)
-- Add an object to each group

for _, barrier in ipairs(barriers) do
	PhysicsService:SetPartCollisionGroup(barrier, barriersGroup)
end
PhysicsService:CollisionGroupSetCollidable(adminsGroup, barriersGroup, false) -- false so they don't collide

game.Players.PlayerAdded:Connect(function(player)
	if table.find(barriers, player.UserId) or table.find(barriers, player.Name) then
		player.CharacterAdded:Connect(function(character)
			for _, instance in ipairs(character:GetDescendants()) do
				if instance:IsA("Part") then
					PhysicsService:SetPartCollisionGroup(instance, adminsGroup)
				end
			end
		end)
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.