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

How Can I Make The Group Door Work As Intended?

It works perfectly except everytime a group member passes through the door it lets a player thats not in the group pass also. I need to type a script where it only opens the door to the player who is in the group. Where do i need to do the function?

GroupId = 3366998

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr:IsInGroup(GroupId) then
        script.Parent.Transparency = 0.7
        script.Parent.CanCollide = false
        wait(2)
        script.Parent.Transparency = 0.15
        script.Parent.CanCollide = true
    end
end)

Than I did this but it still didn't work.

GroupId = 3366998
local Debounce = false;

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and plr:IsInGroup(GroupId) and Debounce == false then
        Debounce = true;
        script.Parent.Transparency = 0.7
        script.Parent.CanCollide = false
        wait(2)
        script.Parent.Transparency = 0.15
        script.Parent.CanCollide = true
        Debounce = false;
    end
end)

1 answer

✓ Accepted answer

You're on the right track, all you need to do is to do something to the player if they aren't in the group. This could be teleport them away from the door or simply killing them, which is the simpler option. For now we'll make it so the player dies, but you can change it as you wish.

GroupId = 3366998

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr:IsInGroup(GroupId) then
		script.Parent.Transparency = 0.7
		script.Parent.CanCollide = false
		wait(2)
		script.Parent.Transparency = 0.15
		script.Parent.CanCollide = true
	else --If the player is not in the group then execute the code below
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then --Check if their humanoid exists
			humanoid.Health = 0 --If it does, then kill them.
		end
    end
end)

Problem solved! If you wanted to make the code even better now, you could leave the door >CanCollide = false as it is no longer necessary.

GroupId = 3366998

script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr:IsInGroup(GroupId) then
		script.Parent.Transparency = 0.7
		wait(2)
		script.Parent.Transparency = 0.15
	else
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
		end
    end
end)

et voila!

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