Magnitude Scripting Help

Im questioning why this script doesnt work. I don’t use magnitude much, but what im trying to achieve is that the player is not allowed to click too far and not too close either. Here’s the code:

						local slideythingie
						slideythingie = mouse.Button1Down:Connect(function()
							local tgt = mouse.Target

							if tgt.Name == "Water" and not Surfing then
								local point = mouse.hit
								if (Root.Position - point.p).magnitude <= 13 and  (Root.Position - point.p).magnitude >= 3 then
									game.ReplicatedStorage.Event:FireServer(char,Humanoid,Root,point,mouse)
									spawn(wedone)									
									slideythingie:Disconnect()
									slideythingie = nil
									print("a")
									wait(1.5)
									for i, v in pairs(workspace.MouseIgnore:GetChildren()) do
										v.CanCollide = true
									end

								else
									print("too far or too close")
								end
							end
						end)

The first part works. The player cannot click too far anymore, but they still can too close.

1 Like

Hey there!

I assume that Root stands for HumanoidRootPart.
As HumanoidRootPart’s position is not exactly on the ground, it takes more magnitude to reach some destination that is placed on a ground.

There are two things which you can try:

  1. Make the (Root.Position - point.p).magnitude >= 3 greater than 3.
  2. Find out how far is the position player clicked on by printing it into console. Just do simple print((Root.Position - point.p).magnitude) before the if statement and based on that raise the number as I said in number 1. :slight_smile:

I do believe there is no problem with the script itself, I think the problem is in numbers.

1 Like

Okay. I will definitely try that out!

1 Like

I know the magnitude i should use now, but the script still doesnt really work.

I’m just curious, what number have you chosen?

4 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee thir tee characters

Oh, 4 feels like still really low number for me. If I would be you, I would probably go for something around 6 or 7. Could you try something like that and see if it works?

Well the thing is, this is just the middle of another script. At the start, the player can click from 8 magnitude thingie away and i just dont want it to be too far and someone gets stuck

(the walkspeed for the player is 0)

And if i set it too high or low, they’ll complain too.

I just want to know why that code doesnt work, i can still click from anywhere near me.

Well, I think the problem is that the number is too low. You could just try raising the number to see if the if statement is fine. If it would be fine, the solution to your problem would be to raise the number, but not drastically tho.

Okay ill try that in the script

1 Like

Yup that worked for some reason! Thank you.
I raised to five.

Yeah, five seems like fine number.
Could you mark my post as a solution so others can orientate easily?

I’m glad it works now,
have a nice rest of your day!

I did already mark it as a solution

Oh sorry, I had to refresh the page, anyways, I was happy to help! Good luck with your game!

1 Like