How can I make this door stay open if a player is inside a part?

I want the door to remain open if the player has opened it and is inside the green zone. I attempted to use Worldroot:GetPartsinPart(), but that method requires me to switch to a localscript and tween service doesn’t work there. Any help would be greatly appreciated

local tweenService = game:GetService("TweenService")

local frame = script.Parent
local model = frame.Parent
local clickDetector = script.Parent:WaitForChild("ClickDetector")

local openSound = frame:WaitForChild("DoorOpen")
local frameClose = model:WaitForChild("DoorFrameClose")
local frameOpen = model:WaitForChild("DoorFrameOpen")

local debounce = true
local opened = false

local function doorOpen()
	if debounce == true then
		debounce = false
		if opened == false then
			opened = true

			openSound:Play()
			tweenService:Create(frame,TweenInfo.new(2.5),{CFrame = frameOpen.CFrame}):Play()

			task.wait(2)
			tweenService:Create(frame,TweenInfo.new(1),{CFrame = frameClose.CFrame}):Play()
			opened = false
		end

		wait(1)
		debounce = true
	end
end


clickDetector.MouseClick:Connect(doorOpen)
2 Likes

You don’t need GetPartsInPart for this to work, just write a Point-in-Box function where the points are the centers of the players’ primary parts. You could even ask Roblox’s Documentation Assistant to write one up for you!

While there are some points found within the box, you just task.wait(x) it out until the next check. If there are no points found then that’s when you move on to closing.

1 Like

How do I get the player’s humanoid? Through game.Players.PlayerAdded:Connect?

1 Like
game.Players.PlayerAdded:Connect(function(plr)
local character = plr.Character or plr.Character.Added:Wait()
local humanoid = character:WaitForChild("Humanoid")
end)
1 Like

Should the script to check the player’s position be placed inside game.Players.PlayerAdded:Connect? If so, how can I ensure that the function runs again to check the position

1 Like

I’ll be honest with you, I have no idea why you need game.Players.PlayerAdded:Connect(), I was just telling you how to get the humanoid.

1 Like

Since it’s a server script that’s the only way I can think of to get the humanoid

1 Like

Use this post to do it very easily

2 Likes

Thank you, this worked perfectly

1 Like

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