Hello,
I’m trying to make my character go toward my mouse, but for some reason it keeps going the same direction how can I fix this? Any help is appreciated.
Code:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local moveValue = 0
--Move
local function onMove(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
moveValue = -100
elseif inputState == Enum.UserInputState.End then
moveValue = 0
end
end
--Update
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
local MoveDirection = moveValue
player.Character.Humanoid:Move(Vector3.new(0,0,MoveDirection), false)
end
end
--Mouse
local function onAim()
if player.Character then
local rootPart = player.Character:FindFirstChild("HumanoidRootPart")
local mouseLocation = Vector3.new(mouse.Hit.X, rootPart.Position.Y, mouse.Hit.Z)
rootPart.CFrame = CFrame.new(rootPart.Position, mouseLocation)
end
end
-- Set up control bindings
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)
ContextActionService:BindAction("Aim", onAim, false, Enum.UserInputType.MouseMovement)
ContextActionService:BindAction("Move", onMove, true, "w", Enum.KeyCode.Up, Enum.KeyCode.DPadUp)
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
local MoveDirection = moveValue
player.Character.Humanoid:Move(Vector3.new(0,0,MoveDirection), false)
end
end
I believe it is because here, you are not adding the vector3 value onto the character’s current position, rather making it always walk towards 0,0,-100
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
local MoveDirection = moveValue
player.Character.Humanoid:Move(Vector3.new(player.Character.PrimaryPart.X,player.Character.PrimaryPart.Y,MoveDirection), false)
end
end
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:wait()
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local mousePos = mouse.Hit
mouse.Button1Down:Connect(function()
local mousePos = mouse.Hit.p
humanoid:WalkToPoint(mousePos)
end)
Simple script which will attempt to make the character walk to a point when they click somewhere on the screen.
After doing some testing I got an error do you know what’s wrong? RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: X is not a valid member of Part "Workspace.SilentSuprion.HumanoidRootPart"
Sorry, i made some mistakes. I had not experimented with retrieving specific X,Y,Z values from a position property beforehand.
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
local MoveDirection = moveValue
player.Character.Humanoid:Move(player.Charater.PrimaryPart.Position + Vector3.new(0,0,MoveDirection), false)
end
end
It still has an error, but no need for saying sorry it’s not your fault I’m confused too. The output is: RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: Charater is not a valid member of Player "Players.SilentSuprion"
I recommend using UserInputService of you do not need to unbind the event as the bind events get messy.
In Fact a much more simpler method would be to check every frame. I don’t think there is a performance drop.
Also the thing to use is IsMouseButtonDown()
Finally PosB + PosA = VectorAB
That is the rule to get a vector between 2 points. Then we can extract that vector.
function Stepped()
if UserInputService:IsMouseButtonDown(LMB) then -- LMB as variable
Humanoid.MoveDirection = (Mouse.Hit.Position - Torso.Position) * Vector3.new(1,0,1)).Unit -- Flooring and Normalizing are unnecessary but I do it here for good practice.
end
end
Additionally check if Vector is NaN in the edge case where Mouse.Hit.Position is nil or is equivalent to Torse.Position
--parent to starterplayerscripts
local Event
Event = game:GetService("RunService").RenderStepped:Connect(function()
if game.Players.LocalPlayer.Character then
game.Players.LocalPlayer.Character:MoveTo(game.Players.LocalPlayer:GetMouse().Hit.p)
end
end)
--Event:Disconnect()
just make sure you don’t hover your mouse over the void or you will p e r i s h
I’m sorry for the confusion, but what I want is the character to go toward the mouse at a constant speed. Right now I’m exploring unbinding actions, but I’m stuck right now.