Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

how do i get the player walk to mouse position?

Asked by
Grazer022 128
4 months ago

what i need is when the mouse is in a position, the player would go to the mouse’s position. The camera is above the player’s head though, which means the player should only move onto either X or Z axis and not Y because the would go upwards. How do i do this? I’m sorry if this is a lot. Thanks!

3 answers

Log in to vote
1
Answered by
rabbi99 535 Moderation Voter
4 months ago
Edited 4 months ago

First you'd need the mouse's position on the workspace. We also need the Character of this player to get to the Humanoid. The Humanoid holds the property WalkToPoint. This will be useful for the script that will manage this system you want. To get this information that script needs, we need to write this in a localscript. This localscript would be located in StarterPlayer > StarterPlayerScripts.

local player = game.Players.Localplayer

local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()
    local ChosenPoint = mouse.Hit
end)

Then, we also want to send this information to a normal script. We're gonna put this script in ServerScriptService. To make this script receive this information we are also gonna make a RemoteEvent. We are gonna name this RemoteEvent "ClickToMove". ClickToMove needs to be located in ReplicatedStorage. But we'd also need to make the localscript send this information:

*The player

*The mouse its position in the workspace

*The character

To do that write this in the localscript:

local rp = game:GetService("ReplicatedStorage") -- We need to get this service first before we do anything with ReplicatedStorage
local ClickToMove = rp:WaitForChild("ClickToMove")

local player = game.Players.Localplayer
local mouse = player:GetMouse()

mouse.Button1Up:Connect(function()
    local ChosenPoint = mouse.Hit
    ClickToMove.FireServer(ChosenPoint) -- This is the information we will send. We don't need to include player in this because it will be included automatically.
end)

Great, now we've sent this information! But now we need to make the script located in the ServerScriptService to receive and process this information.

First we want it to collect it of course:

local rp = game:GetService("ReplicatedStorage")
local ClickToMove = rp:WaitForChild("ClickToMove")

ClickToMove.OnServerEvent:Connect(function(player, ChosenPoint, character) -- This is the information we sent from the localscript. We need to have this in this exact order!


end)

Nice! Now we have collected the information from the localscript. Now we can finally process this information in the script that's located in the ServerScriptService

local rp = game:GetService("ReplicatedStorage")
local ClickToMove = rp:WaitForChild("ClickToMove")

ClickToMove.OnServerEvent:Connect(function(player, ChosenPoint, character) -- This is the information we sent from the localscript. We need to have this in this exact order!
    local character = player.Character
    -- Since you wanted only on X and Z we are gonna make it so.
    local PointY = character.HumanoidRootPart.Position.Y -- Like this the Y position will stay the same.
    local PointX = ChosenPoint.X
    local PointZ = ChosenPoint.Z
    character.Humanoid.WalkToPoint = Vector3.new(PointX, PointY, PointZ)-- Here we told the Humanoid to walk to the point we clicked. 
end)

That's it! I hope you liked my tutorial!

1
THAT’S A GOOD EXPLANATION! thank u so muchh! I’m not gonna copy it though since i wanna script for myself but I understand a lot! Thanks! Grazer022 128 — 4mo
0
Good work! rabbi99 535 — 4mo
Ad
Log in to vote
0
Answered by 4 months ago

i don't know how to lock movement mode to click to move but there's a movement mode called Click to move

Log in to vote
0
Answered by 4 months ago

Put a Local script in starterPack

local Player = game.Players.LocalPlayer
wait(1)
local Humanoid = Player.Character.Humanoid
local Mouse = Player:GetMouse()
Mouse.MouseButton1Down:Connect(function()
        Humanoid:MoveTo(Mouse.Hit.p)
end)

See if this works.

Answer this question