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
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
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.
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)
--//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)
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)