CFrame acting weird

Basically im using cframe to move a characer up and down.

heres my code:

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		upRS = RS.RenderStepped:Connect(function()
			player:SetPrimaryPartCFrame(player.Head.CFrame*CFrame.new(0,-1,0))
		end)
	end
	if input.KeyCode == Enum.KeyCode.Down then
		downRS = RS.RenderStepped:Connect(function()
			player:SetPrimaryPartCFrame(player.Head.CFrame*CFrame.new(0,1,0))
		end)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		upRS:Disconnect()
	end
	if input.KeyCode == Enum.KeyCode.Down then
		downRS:Disconnect()
	end
end)

The problem is that when i press the down arrow the player moves upwards faster than the up arrow key press

the only error that comes up is telling me that the downRS is nil even though i should not be nil

any help?

HumanoidRootPart.CFrame += Vector3.new(0,height,0) i think this is the proper way of moving in Y axis

Position + cframe doesnt add up

and no you cannot just do cframe + cframe

Im not saying Position + CFrame im saying HumanoidRootPart.CFrame = HRP.CFrame + Vector3.new(0,height,0) i have done this for years ik what im saying

That quite literally is cframe + pos

HRP.CFrame + Vector3.new(0,height,0)

anyways =+ seems to be not working, it has a red line under it.

player.HumanoidRootPart.CFrame += Vector3.new(0,1,0)

am i doing something wrong?

Yes player isnt the character you should do character = game.Players:GetPlayerFromCharacter(player) then do the same with character make sure you have player and character

Edit: And CFrame + Vector3.new is 100% right and += couldnt be problem here

+= isn’t supported in Lua. You have to do x = x + y instead. Can you elaborate on what you mean by it goes up faster? As for the nil error, I’d try adding a check in the function to see if upRS and downRS are nil before disconnecting them. For example:

if input.KeyCode == Enum.KeyCode.Up and upRS ~= nil then
    upRS:Disconnect()
end

Actually, if you’re saying what I think you’re saying then it’s probably caused by this line:

player:SetPrimaryPartCFrame(player.Head.CFrame*CFrame.new(0,1,0))

You’re taking the player’s head and adding 1 on the Y axis to it, then setting the player’s root part there. Given your head is father up your body than your torso (root part), it’s going to move up faster.

Try replacing that with something like this:

player:PivotTo(player:GetPivot() * CFrame.new(0, 1, 0))

Basicly he teleports player but player is instance and its not the Character so he gotta do game.Players:GetPlayerFromCharacter(player)

no thats not the case.
he script works just fine when i press the up arrow

+= is supported in luau. Try doing a simple command in the command bar like this to test it out:

local x = 0
x += 1

print(x) --//Prints 1

i tried all of your advices yet the script doesnt work diffrently.

when i change the cframe value to -1 it behaves like an up arrow press, im clueless about why his happens.

I must have missed when that was added, thanks.

1 Like

Did you try this


UIS.InputBegan:Connect(function(input)

    if input.KeyCode == Enum.KeyCode.Up then

        upRS = RS.RenderStepped:Connect(function()

            game.Players:GetPlayerFromCharacter(player).HumanoidRootPart.CFrame += Vector3.new(0,1,0)

        end)

    end

    if input.KeyCode == Enum.KeyCode.Down then

        downRS = RS.RenderStepped:Connect(function()

            game.Players:GetPlayerFromCharacter(player).HumanoidRootPart.CFrame += Vector3.new(0,1,0)

        end)

    end

end)

UIS.InputEnded:Connect(function(input)

    if input.KeyCode == Enum.KeyCode.Up then

        upRS:Disconnect()

    end

    if input.KeyCode == Enum.KeyCode.Down then

        downRS:Disconnect()

    end

end)

This should work:

--//Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

--//Tables
local Connections = {}

--//Functions
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		Connections.upRS = RunService.RenderStepped:Connect(function()
			Character:PivotTo(Character.Head.CFrame + Vector3.new(0, 1, 0))
		end)
	end

	if input.KeyCode == Enum.KeyCode.Down then
		Connections.downRS = RunService.RenderStepped:Connect(function()
			Character:PivotTo(Character.Head.CFrame - Vector3.new(0, 1, 0))
		end)
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		Connections.upRS:Disconnect()
	end
	
	if input.KeyCode == Enum.KeyCode.Down then
		Connections.downRS:Disconnect()
	end
end)

You’re subtracting Vector3s from CFrames.

im telling you player is the character.
also changing the hrp pos does the same.
why does this even happen???

You can do that. CFrames are actually comprised of positional (a vector3) and a rotational (also a vector3) value. Test it out for yourself.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up then
		upRS = RS.RenderStepped:Connect(function()
			player:PivotTo(player:GetPivot() * CFrame.new(0, -1, 0))
		end)
	end
	if input.KeyCode == Enum.KeyCode.Down then
		downRS = RS.RenderStepped:Connect(function()
			player:PivotTo(player:GetPivot() * CFrame.new(0, 1, 0))
		end)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Up and upRS ~= nil then
		upRS:Disconnect()
	end
	if input.KeyCode == Enum.KeyCode.Down and downRS ~= nil then
		downRS:Disconnect()
	end
end)

Here’s my try.

This game has changed so much since I started playing lol.