This code works but the controls are very buggy or glitchy and slow.
Is there anything missing?
If this code is fixed then my game can finally work for console players.
print("We're a gamepad user")
print("We're a gamepad user")
local xmove = "none"
local ymove = "none"
local zmove = "none"
local function handleAction(actionName, bool)
warn(actionName, bool)
if actionName == "Forward" then
if bool == true then
zmove = "forward"
if math.abs(doubleW - tick()) < 0.3 then
coroutine.wrap(function()
if cart:FindFirstChild("Rocket") and cart:FindFirstChild("Rocket").FUEL.Value > 0 then
cart:FindFirstChild("Rocket").RThruster.Boost:Play()
cart:FindFirstChild("Rocket").fx:FireServer(true)
cart:FindFirstChild("Rocket").RThruster.Sparkles.Enabled = true
while zmove == "forward" and cart:FindFirstChild("Rocket") and cart:FindFirstChild("Rocket").FUEL.Value > 0 do
if cart.PrimaryPart.AssemblyLinearVelocity.Magnitude < 400 then
cart.PrimaryPart:ApplyImpulse( cart.PrimaryPart.CFrame.LookVector * massofcart * 0.976 )
elseif cart.PrimaryPart.AssemblyLinearVelocity.Magnitude > 400 then
cart.PrimaryPart:ApplyImpulse( cart.PrimaryPart.CFrame.LookVector * massofcart * 0.316 )
end
task.wait()
end
cart:FindFirstChild("Rocket").fx:FireServer(false)
end
end)()
end
doubleW = tick()
local forwardanim = char.Humanoid:LoadAnimation(animsfolder.front)
while zmove == "forward" do
cartfunctions[cart.Name]["GoForward"](cart, char, cartminforce, cartmaxforce)
forwardanim:Play()
if zmove ~= "forward" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
forwardanim:Stop()
wait(3)
forwardanim:Destroy()
else
if zmove == "forward" then
zmove = "none"
end
end
end
if actionName == "Backward" then
if bool == true then
zmove = "backward"
local forwardanim = char.Humanoid:LoadAnimation(animsfolder.back)
while zmove == "backward" do
cartfunctions[cart.Name]["GoBackward"](cart, char, cartminforce, cartmaxforce)
forwardanim:Play()
if zmove ~= "backward" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
forwardanim:Stop()
wait(3)
forwardanim:Destroy()
else
if zmove == "backward" then
zmove = "none"
end
end
end
if actionName == "Left" then
if bool == true then
xmove = "left"
local forwardanim = char.Humanoid:LoadAnimation(animsfolder.left)
while xmove == "left" do
cartfunctions[cart.Name]["GoLeft"](cart, char, cartminforce, cartmaxforce)
forwardanim:Play()
if xmove ~= "left" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
forwardanim:Stop()
wait(3)
forwardanim:Destroy()
else
if xmove == "left" then
xmove = "none"
end
end
end
if actionName == "Right" then
if bool == true then
xmove = "right"
local forwardanim = char.Humanoid:LoadAnimation(animsfolder.right)
while xmove == "right" do
cartfunctions[cart.Name]["GoRight"](cart, char, cartminforce, cartmaxforce)
forwardanim:Play()
if xmove ~= "right" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
forwardanim:Stop()
wait(3)
forwardanim:Destroy()
else
if xmove == "right" then
xmove = "none"
end
end
end
if flight then
if actionName == "Up" then
if bool == true then
ymove = "up"
while ymove == "up" do
cartfunctions[cart.Name]["GoUp"](cart, char, cartminforce, cartmaxforce)
if ymove ~= "up" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
wait(3)
else
if ymove == "up" then
ymove = "none"
end
end
end
if actionName == "Down" then
if bool == true then
ymove = "down"
while ymove == "down" do
cartfunctions[cart.Name]["GoDown"](cart, char, cartminforce, cartmaxforce)
if ymove ~= "down" then
break
end
wait(.1)
end
cartfunctions[cart.Name]["Normal"](cart )
wait(3)
else
if ymove == "down" then
ymove = "none"
end
end
end
end
end
test = uis.InputBegan:Connect(function(input, g)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.ButtonR2 then
handleAction("Forward", true)
end
if input.KeyCode == Enum.KeyCode.ButtonL2 then
handleAction("Backward", true)
end
end
end)
test2 = uis.InputEnded:Connect(function(input, g)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.ButtonR2 then
handleAction("Forward", false)
end
if input.KeyCode == Enum.KeyCode.ButtonL2 then
handleAction("Backward", false)
end
end
end)
table.insert(mobilecontrols, test)
table.insert(mobilecontrols, test2)
local gamepadpos = nil
thumbstickcontrols = uis.InputChanged:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
gamepadpos = input.Position
warn(gamepadpos)
if gamepadpos.Y > 0.5 then
zmove = "up"
-- cart bodyvel y force is set to 0.
elseif gamepadpos.Y < -0.5 then
zmove ="down"
elseif gamepadpos.Y >= -0.5 and gamepadpos.Y <= 0.5 then
zmove = "none"
end
if gamepadpos.X > 0.5 then
xmove = "right"
-- cart bodyvel y force is set to 0.
elseif gamepadpos.X < -0.5 then
xmove ="left"
elseif gamepadpos.X >= -0.5 and gamepadpos.X <= 0.5 then
xmove = "none"
end
end
end
end)
warn(thumbstickcontrols, "exists!")
local xanim = nil
local zanim = nil
coroutine.wrap(function()
while cart do
task.wait(.1)
if xmove == "left" then
if xanim then
xanim:Stop()
xanim:Destroy()
end
xanim = char.Humanoid:LoadAnimation(animsfolder.left)
cartfunctions[cart.Name]["GoLeft"](cart, char)
xanim:Play()
elseif xmove == "right" then
if xanim then
xanim:Stop()
xanim:Destroy()
end
xanim = char.Humanoid:LoadAnimation(animsfolder.right)
cartfunctions[cart.Name]["GoRight"](cart, char)
xanim:Play()
else
if xanim then
xanim:Stop()
xanim:Destroy()
end
cartfunctions[cart.Name]["Normal"](cart )
end
if zmove == "up" then
if zanim then
zanim:Stop()
zanim:Destroy()
end
zanim = char.Humanoid:LoadAnimation(animsfolder.left)
cartfunctions[cart.Name]["GoLeft"](cart, char)
zanim:Play()
elseif zmove == "down" then
if zanim then
zanim:Stop()
zanim:Destroy()
end
zanim = char.Humanoid:LoadAnimation(animsfolder.right)
cartfunctions[cart.Name]["GoDown"](cart, desiredyvel)
zanim:Play()
else
if zanim then
zanim:Stop()
zanim:Destroy()
end
cart:FindFirstChild("BodyVelocity", true).Velocity = cart:FindFirstChild("BodyVelocity", true).Velocity + Vector3.new(0,desiredyvel,0)
end
end
end)()
It prints the input.Position btw.
It’s meant to be like button R2 and L2 is to accelerate and decelerate and then the thumbstick is to go either up or down left right etc