How would I make a player move with the mouse

I am making a hockey game where as the goalie, wherever you put your mouse, you move to. I am having difficulty making/finding a script that’d help me do this (not rotating, but actually moving the character)

Version that moves when mouse is clicked

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	local target = Mouse.Hit.Position

	Player.Character.Humanoid:MoveTo(Vector3.new(target.X, target.Y, target.Z))

end)

Version that moves when mouse is moved

local RunService = game:GetService("RunService")

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

RunService.RenderStepped:Connect(function()
	local target = Mouse.Hit.Position

	Player.Character.Humanoid:MoveTo(Vector3.new(target.X, target.Y, target.Z))

end)

Hope this helps

Edit:
If you want to limit it to one axis, you can replace the other axes with 0.
eg:

Player.Character.Humanoid:MoveTo(Vector3.new(target.X, 0, 0))
3 Likes

Thanks a lot! This really helped with making the game work.

1 Like