Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why will my player not move and how do I make it follow the mouse?

Asked by
joeldes 201 Moderation Voter
6 years ago

Hello, I am new to the community here. I have been recently been working on a game where I have a sphere that is the player. I made it so that you could view the player from perfect top down. Unfortunately, when I did this the player stopped moving with WASD keys. I noticed that as soon as I change the Vector3 to not point the CFrame all the way down the character moves again but in a diagonal and inverted way.

Here's my code:

print("Intalized Camera Script")

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local OFFSET = Vector3.new(0, 400, 0)
local FOV = 30

local player = Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

camera.FieldOfView = FOV

local function onRenderStep()
    local character = player.Character
    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            local playerPos = humanoidRootPart.Position
            local cameraPos = playerPos + OFFSET
            camera.CoordinateFrame = CFrame.new(cameraPos, playerPos)
        end
    end
end

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)

I followed the Roblox Wiki tutorial and modified it to fit my needs. I have been searching for hours for answers. Maybe I am blind and can't read or I am finding nothing.

Basically I need help composing a script that will allow my player to move by following the direction the mouse is pointing on the screen. I am trying to keep my view to be perfectly looking straight down.

Please note that I am new to Lua completely and am not looking to be spoon fed the script. But please be as detailed as possible so I can learn :D.

Thanks!

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

This script will just automatically move the character towards the mouse, if that’s what you are looking for.

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Move:Connect(function()
    game.Players.LocalPlayer.Character.Humanoid:MoveTo(mouse.Hit.p)
end)
0
Thank you so much. That works! But for some reason it only works in the studio and not as a published game joeldes 201 — 6y
0
You should put this in a local script inside StarterGui or somewhere else that is individual to each player. mattscy 3725 — 6y
Ad

Answer this question