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

How do I use magnitude effectively by using it to open this door?

I already made a script that uses click detection for my door. I want to change this though, by making it so that if the player walks in within 5 studs, the door will open without having to press the click detector. I find that the click detector isn't the best way to open a door in my opinion; and I'd like to open a door without even clicking on the door, but just walking towards/near it. When you're within 5 studs, the door should stay open; but I don't know how to do that. Here is my script:

local tween = game:GetService("TweenService")

local center = script.Parent.Center
local door = "Closed"

local CF = Instance.new("CFrameValue")
CF.Value = center.CFrame
CF.Changed:Connect(function()
	center.CFrame = CF.Value
end)

local debounce = false
script.Parent.Door.ClickDetector.MouseClick:Connect(function()
	if debounce == true then return end
	debounce = true
	if door == "closed" then
		tween:Create(CF, TweenInfo.new(3, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(90),math.rad(0))}):play()
		door = "open"
	else
		tween:Create(CF, TweenInfo.new(3, Enum.EasingStyle.Quart), {Value = center.CFrame * CFrame.Angles(math.rad(0),math.rad(-90),math.rad(0))}):play()
		door = "closed"
	end
	wait(3)
	debounce = false
end)

2 answers

✓ Accepted answer
local Players = game:GetService("Players")
local DoorPart = workspace.Part

while wait() do
  for _,Player in next, Players:GetPlayers() do 
	
  local Character = Player.Character or Player.CharacterAdded:wait() 
  local HumanoidRootPart = Character.HumanoidRootPart --[[the object we'll be tracking if its close or not]]

  local A,B = HumanoidRootPart.Position, DoorPart.Position 
  local Distance = (A - B).Magnitude -- magnitude calculation

  if Distance <= 5 then -- 5 is the stud distance from the door part
	print('close to door')
	-- from here run your door operation code
   else
	print('not close to door')
   end
  end
end

If this helps your needs then you're welcome.

2 comments

0

Yes it works, but there's this really strange bug. Do you have discord so that I can send you it, and that you can help me? Thanks.

0

You should use RunService which is basically a nanosecond while loop that doesn't crash your game

One way you can do it is to check the distance between the player and the door locally, maybe using run service, and open the door on the server side

and fire the server if the player is within 5 studs which will open the door

also use 'if' to check if the door is already openining/open so that the client won't keep firing the server

for closing it, just check if player is farther than 5 magnitude and fire server if the door is open to close it and dont fire if the door is already closing/closed

3 comments

0

It doesn't work when I try to check the distance locally, but it works when I try to do it on the server.

0

doesnt work? explain what u mean by doesnt work tho?

0

You could make a "Get all object in blah studs" radius but im not sure if thats gonna lag up the game if you have big buildings

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