What I was trying to do was fire RemoteEvent when you get close to door and make it open. However, the door just keeps opening and closing when I get close enough. How do I fix this?

game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
while true do
	for _, Door in pairs(workspace.Door:GetChildren()) do
		local Mag = (Door.Center.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		if Mag <= Door.Range.Value then
			Door.DoorEvent:FireServer()
			print("Open")
			wait()
			break
		end
	end
	wait()
end

Script below is inside the Door Model which is Inside Folder. Main is literally Door itself, and Center is a little part inside Main. Range is an IntValue and the Value is 6.

local Can = true
local Open = false
script.Parent.PrimaryPart = script.Parent.Hinge

script.Parent.DoorEvent.OnServerEvent:Connect(function(Player)
	local Mag = (script.Parent.Center.Position - Player.Character.HumanoidRootPart.Position).magnitude
	if Mag <= script.Parent.Range.Value then
		if Can then
			Can = false
			if Open == false then
				local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(90),0)
				for i = 0, 1, 0.1 do
					local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish,i)
					script.Parent:SetPrimaryPartCFrame(cfm)
					wait()
				end
				Open = true
				print(Open)
			else
				Open = false
				local finish = script.Parent.PrimaryPart.CFrame * CFrame.Angles(0,-math.rad(90),0)
				for i = 0, 1, 0.1 do
					local cfm = script.Parent.PrimaryPart.CFrame:lerp(finish,i)
					script.Parent:SetPrimaryPartCFrame(cfm)
					wait()
				end
			end
			Can = true
		end
	end	
end)

3 comments

1

Why are you using a RemoteEvent for this? You do not need to have a RemoteEvent.

0

I believe the reason for that is because they want the client to do the calculations(proximity of player to door) and the server to replicate it to the other clients as to reduce the amount of server work.

0

How do I make it work without RemoteEvent though?