Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

0
make sure a player is touches the door raid6n 2196 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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)
Ad
Log in to vote
0
Answered by
tomekcz 174
4 years ago

Collision groups

Answer this question