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

How can I force a player to go left?

Asked by 5 years ago

Im trying to force one player to go left. I tried using CFrame, but that makes the player go left in the games perspective. I want the player to go left through his. Is there a way to do this? Here is my script:

game.ReplicatedStorage.Roll.OnServerEvent:Connect(function(plr, char)
    local uppertorso = char.UpperTorso
    char.CFrame = char.CFrame - Vector3.new(10,0,0)
end)

2 answers

Log in to vote
1
Answered by 5 years ago

Hey Captain,

I came up with two methods to make a smooth transition towards the left of the player. One makes use of a BodyPosition object, and is more unnatural but it helps you take almost complete control of the character so you can move the player wherever you want to and the player can not interfere with it. Then, the second one is much more natural, and basically the character walks to the position 5 studs to the left.

Here, let me demonstrate the code:

local players = game:GetService("Players")
local player = players.LocalPlayer
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(obj, gp)
    if obj.KeyCode == Enum.KeyCode.Z and not gp then
        local char = player.Character or player.CharacterAdded:Wait() -- Gets the character of the player.
        local torso = char:FindFirstChild("HumanoidRootPart") -- The humanoid root part of the cahracter
        local pos = (torso.CFrame:pointToWorldSpace(Vector3.new(-5, 0, 0))) -- A position from the world space, 5 studs to the left of the humanoid root part.

        ------------- METHOD ONE -------------

        local bp = Instance.new("BodyPosition") -- Make a Body Position to move the player.
        bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- Set the max force to infinity
        bp.Position = pos -- Set the position of the body position to move towards.
        bp.Parent = torso -- Set the parent so that it moves the parent.
        repeat -- Meant to wait until the character reaches the position, so we can destroy it afterwards.
            wait()
        until ((torso.Position - pos).magnitude) < 1

        bp:Destroy() -- Destroys the body position so that the player can move.

        -------------    ---     -------------

        ------------- METHOD TWO -------------

        local hum = char:WaitForChild("Humanoid") -- Identifies the humanoid of the player.

        hum.WalkToPoint = pos -- Changes the walking point of the humanoid to walk towards that position.

        -------------    ---     -------------

    end
end)

I hope I was of some help and have a nice day/night.

Thanks,

Best regards,

KingLoneCat

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

just use CFrame.RightVector and multiply it by a negative number, since it'll be the opposite of RightVector, as theres no LeftVector. also why are use creating a variable thats defined as the player's UpperTorso, and not using it.

game.ReplicatedStorage.Roll.OnServerEvent:Connect(function(plr, char)
    local uppertorso = char.UpperTorso

   uppertorso.CFrame = uppertorso.CFrame + uppertorso.CFrame.RightVector * -10
end)

also you cant send instances through arguments in OnServerEvent, so just set the cframe on the client, because it would replicate. or if you wanted it to move to the left instead of teleporting use MoveTo().

character.Humanoid:MoveTo(character.HumanoidRootPart.CFrame.Position + character.HumanoidRootPart.CFrame.RightVector * -10)

Answer this question