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

Door continues to open even if player is not near?

So, I'm trying to make it so when a player is near a door, it opens, and when no-one is around, it is closed, but it's not working and it gets moved up (which is not in any script) and keeps opening even if its reached its max. So, whats wrong?

local sp = script.Parent
local trigger = sp.trigger
local left = sp.left
local right = sp.right
local openOrClosed = sp.openOrClosed

local leftPosClosedX = 4.28
local leftPosOpenX = 10.28
local rightPosClosedX = -1.26
local rightPosOpenX = -7.26

trigger.Touched:connect(function(hit)
	openOrClosed.Value = true
end)

trigger.TouchEnded:connect(function(hit)
	openOrClosed.Value = false
end)

while wait(0.1) do
	if openOrClosed.Value == false then
		if right.Position.X ~= rightPosClosedX and left.Position.X ~= leftPosClosedX then
			repeat
				left.Position = Vector3.new(left.Position.X - 0.1, left.Position.Y, left.Position.Z)	
				right.Position = Vector3.new(right.Position.X + 0.1, right.Position.Y, right.Position.Z)
				wait(0.1)
			until
			right.Position.X == rightPosClosedX and left.Position.X == leftPosClosedX
		end
	elseif openOrClosed.Value == true then
		if right.Position.X ~= rightPosOpenX and left.Position.X ~= leftPosOpenX then
			repeat
				left.Position = Vector3.new(left.Positon.X + 0.1, left.Position.Y, left.Positon.Z)
				right.Position = Vector3.new(right.Position.X - 0.1, right.Position.Y, right.Position.Z)
				wait(0.1)
			until
			right.Position.X == rightPosOpenX and left.Position.X == leftPosOpenX
		end
	end
end

1 answer

You can use the Magnitude like me and make your Door work.

Today I created something similar to this but did otherwise.

I created two doors, one open and one closed, used magnitude for when I get the block the Closed Door model Stay invisible and Opened Door stay Visible, the version and more simplificated that his and which does not need a lot of work and moreover you you can leave her with many details.

The Script:

IntPart = script.Parent.Interact
Children = script.Parent.Parent.Door:GetChildren()
Children2 = script.Parent.Parent.OpenedDoor:GetChildren()

while wait() do
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - IntPart.Position).magnitude <= 10 then
			v.PlayerGui.IntGuis[IntPart.Type.Value].Visible = true
			script.Key.Disabled = false
			
			for i = 1,#Children do
				if Children[i].ClassName == "Part" then
					Children[i].Transparency = 1
				end
			end
			for i = 1,#Children2 do
				if Children2[i].ClassName == "Part" or Children2[i].ClassName == "UnionOperation" then
					Children2[i].Transparency = 0
				end
			end
		else
			v.PlayerGui.IntGuis[IntPart.Type.Value].Visible = false
			script.Key.Disabled = true
			
			for i = 1,#Children do
				if Children[i].ClassName == "Part" then
					Children[i].Transparency = 0
				end
			end
			for i = 1,#Children2 do
				if Children2[i].ClassName == "Part" or Children2[i].ClassName == "UnionOperation" then
					Children2[i].Transparency = 1
				end
			end
		end
	end
end

This is your script, see if it works

local sp = script.Parent
local trigger = sp.trigger
local left = sp.left
local right = sp.right
local openOrClosed = sp.openOrClosed

local leftPosClosedX = 4.28
local leftPosOpenX = 10.28
local rightPosClosedX = -1.26
local rightPosOpenX = -7.26

while wait() do
	for i,v in pairs(game.Players:GetChildren()) do
		if (v.Character.Torso.Position - trigger.Position).magnitude <= 10 then
	        if right.Position.X ~= rightPosClosedX and left.Position.X ~= leftPosClosedX then
	        	repeat
		                left.Position = Vector3.new(left.Position.X - 0.1, left.Position.Y, left.Position.Z)    
		                right.Position = Vector3.new(right.Position.X + 0.1, right.Position.Y, right.Position.Z)
	               wait(0.1)
	            until
	            right.Position.X == rightPosClosedX and left.Position.X == leftPosClosedX
	        end
	    else
	        if right.Position.X ~= rightPosOpenX and left.Position.X ~= leftPosOpenX then
	            repeat
	                left.Position = Vector3.new(left.Positon.X + 0.1, left.Position.Y, left.Positon.Z)
	                right.Position = Vector3.new(right.Position.X - 0.1, right.Position.Y, right.Position.Z)
	                wait(0.1)
	            until
	            right.Position.X == rightPosOpenX and left.Position.X == leftPosOpenX
	        end
	    end
	end
end

If it dont worked let me take a little longer to fix.

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