Group Only Door Function

I’m new to scripting and I know this script is very easy and simple but I’m having a problem.
The script functions as it should, but if someone isn’t in the group and walks right behind the player in the group, that person can enter the door. I was wondering how to make it local or client-sided so this doesn’t happen.

local groupID = 32857623  -- The group ID the player must be in
local groupMembers = {} -- Table to store group member states

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	if player then
		if player:IsInGroup(groupID) then
			groupMembers[player.UserId] = true
			script.Parent.CanCollide = false
		else
			groupMembers[player.UserId] = false
			script.Parent.CanCollide = true
		end
	end
end)

-- Update collision state when player leaves
game.Players.PlayerRemoving:Connect(function(player)
	groupMembers[player.UserId] = nil
end)

-- Continuous collision state check
while true do
	for userId, inGroup in pairs(groupMembers) do
		local player = game.Players:GetPlayerByUserId(userId)
		if player and inGroup then
			script.Parent.CanCollide = false
			break
		end
	end
	wait(1)
end
local Player = game:GetService("Players").LocalPlayer
local Door = --// path to the door 

Door.Touched:Connect(function(Hit)
	if (Hit.Parent.Name == Player.Name) and (Player:IsInGroup(32857623) == true) then 
		Door.CanCollide = false
	end
end)

localscript in starterplayer → starterplayerscripts

2 Likes

thanks so much. couldnt even find a vid about this

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.