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

How do I make a character move 20 studs the direction the Torso is facing?

Asked by 8 years ago

This seems like a basic question to me. I've tried multiple this.

I've tried,

local tool = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local Torso = char:WaitForChild("Torso")
local mouse = plr:GetMouse()
tool.Equipped:connect(function()
    tool.Activated:connect(function()
        Torso.CFrame = (Torso.CFrame*CFrame.new(Torso.CFrame.lookVector*20))
    end)
end)
In this one I tried to use math. The player does move, just not always in the right direction.

I've also tried,

local tool = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local Torso = char:WaitForChild("Torso")
tool.Equipped:connect(function()
    tool.Activated:connect(function()
        Torso.CFrame = Torso.CFrame:toWorldSpace(CFrame.new(Torso.CFrame.lookVector))
    end)
end)
In this one I tried using :toWorldSpace() and this had almost the same result as the other attempt.

There were no errors. The only problem is the way it moves the player. Sometimes the player can get moved backwards and other times the player moves sideways. It only seems to work if the player is facing a single direction.

Any help would be awesome.

Thank you.

1 answer

Log in to vote
4
Answered by 8 years ago

CFrames

To move a CFrame 20 studs forwards, you need to use a Z of -20. Very odd.

Torso.CFrame = Torso.CFrame * CFrame.new(0,0,-20)

Make sure they don't get stuck in a wall ^^

Ad

Answer this question