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

How to check distance between your player and Mouse position?

I don't Know what do to with Mouse position so I left the Position checker if Mouse.Position > 500 then end.

-- Variables --
local replicated = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
local UserInputService = game:GetService("UserInputService")
local humanoid = player.Character:WaitForChild("Humanoid")

Mouse = player:GetMouse()
Debounce = true
wait(1)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Mouse.Position > 500 then end
		else
		player.Character:MoveTo(Mouse.Hit.p)
	end	
end)

1 answer

✓ Accepted answer

The answer is quite simple; using the magnitude properties of a Vector3 you can calculate the distance between two positions:

local distance = (part1.Position - part2.Position).magnitude

Here's your script with the extra snippet of code:

-- Variables --
local replicated = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
local UserInputService = game:GetService("UserInputService")
local humanoid = player.Character:WaitForChild("Humanoid")

Mouse = player:GetMouse()
Debounce = true
wait(1)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if (Mouse.Hit.p - humanoid.Parent.HumanoidRootPart.Position).magnitude > 500 then end
        else
        player.Character:MoveTo(Mouse.Hit.p)
    end 
end)

Hope this helps!

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