Your best option is to use a LocalScript instead since you want only specific players to go through it.
However, if you really need to have it Server-Sided for whatever reason, you can also rely on the Touched event.
I scripted this some time ago, but I’m sure you can figure out something useful from it.
local part = script.Parent -- the part that changes its CanCollide behavior
local Group = 0 --
local minimumRankLevel = 0 -- you can modify these settings if you want to make it User-Specific
local db = false
local db_2 = false
local function onPartTouched(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
repeat wait() until humanoid
local character = humanoid.Parent
local plr = game.Players:GetPlayerFromCharacter(character)
if humanoid then
if plr:IsInGroup(Group) and plr:GetRankInGroup(Group) >= minimumRankLevel then
if db_2 == false then
db_2 = true
print(plr.Name.." is in the group and their rank is "..plr:GetRoleInGroup(Group)..".")
wait(0.5)
part.CanCollide = false
wait(2)
part.CanCollide = true
db_2 = false
end
end
elseif db == false then
db = true
print(plr.Name.." is not allowed to go through this area! ("..plr:GetRoleInGroup(Group)..")")
plr:LoadCharacter()
wait(1)
db = false
end
end
part.Touched:Connect(onPartTouched)
It’s not perfect, but what it basically does is check if the player is in the group and their rank, if it matches with the variables then it will let them in. If not, it will respawn them.
Good luck!