Problem with magnitude for my door script

Here’s my code :

local inputservice = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()  


inputservice.InputBegan:connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Door in pairs(workspace.Door["Large Doors"]:GetChildren()) do
				for _,Door2 in pairs(Door:GetChildren()) do
					for _,Door3 in pairs(Door2:GetChildren()) do
--Main issue is here
						if mouse.Target and mouse.Target.Name == "Door" then	
						local Mag = (Door3.Center.Position-game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position).magnitude
							if Mag <= Door3.Range.Value then	
								Door3.Event:FireServer()
								break
							end
						end
					end
				end
			end
		end
	end
end)

image

(This isn’t all the code. I just provided all the code and pictures that are the problem. I also tested my doors without all the other code and it worked the same just with or without the GUI)

Its hard to explain, but basically I find if my mouse is over the the doors part then I go to
magnitude to find if I’m close enough to active the door. However I ran into some issues…


I have no idea what I should do about this, any help is appreciated!

3 Likes

You use a capital letter when using the Magnitude function.

2 Likes

Also, are there any output errors?

2 Likes

Nope, not even a single error.

1 Like

Okay then, try to print out the magnitude, tell me what it says.

2 Likes

It’s just the magnitude of my root part and the doors center image

1 Like

the range is 3, basically the max amount of studs I can be away from it.

2 Likes

Try doing _1 _2 _3 instead of having the same _
Also, did you define door2 and door3 I don’t see where the variable door 2 and 3 is.

1 Like

Im getting the children of the model and folders Its called Door.
Then I’d have to do that for about 50 other doors

But, I don’t see where you defined the Door2
U just say Door2:GetChildren()

			for _,Door in pairs(workspace.Door["Large Doors"]:GetChildren()) do
				for _,Door2 in pairs(Door:GetChildren()) do
					for _,Door3 in pairs(Door2:GetChildren()) do

image

Try doing workspace.Door.Door2:GetChildren()

1 Like