I’m trying to make a custom controls for the character movements because that way i can have more control over the way the characters act and maybe try to implement it to AIs as well but at the moment it is not very smooth
This code atm is client side only and i will make it server side later and what it is supposed to do is simply make the player move and i will add some additional features later but im facing an issue which causes the character to teleport instead which is not what is supposed to be happening. I tried doing it by lerping the humanoidrootpart which used to work before that when i was doing some experimental codes but doesnt work properly now.
I have tried tweaking with the numbers and i also tried use for i = 0,1,0.1 do
after the loop that should move the character without stopping but non of these 2 methods did the work for me.
This is the script(the script is in StarterCharacterScripts:
wait() -- for whatever reasons it gives me error when i dont use this
local uis = game:GetService("UserInputService")
local forward = script.Parent.Movements.Forward
local backward = script.Parent.Movements.Backward
local rightside = script.Parent.Movements.Rightside
local leftside = script.Parent.Movements.Leftside
local jump = script.Parent.Movements.Jump
-- movements
local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
Controls:Disable()
-- disable already existing controls
uis.InputBegan:Connect(function(i,s)
if s == false then
if i.KeyCode == Enum.KeyCode.W then
forward.Value = true
end
if i.KeyCode == Enum.KeyCode.S then
backward.Value = true
end
if i.KeyCode == Enum.KeyCode.D then
rightside.Value = true
end
if i.KeyCode == Enum.KeyCode.A then
leftside.Value = true
end
if i.KeyCode == Enum.KeyCode.Space then
jump.Value = true
end
end
end)
-- movements activated when the buttons are pressed
uis.InputEnded:Connect(function(i,s)
if s == false then
if i.KeyCode == Enum.KeyCode.W then
forward.Value = false
end
if i.KeyCode == Enum.KeyCode.S then
backward.Value = false
end
if i.KeyCode == Enum.KeyCode.D then
rightside.Value = false
end
if i.KeyCode == Enum.KeyCode.A then
leftside.Value = false
end
if i.KeyCode == Enum.KeyCode.Space then
jump.Value = false
end
end
end)
-- movements deactivated when the buttons are not pressed
forward.Changed:Connect(function()
if forward.Value == true then
repeat
wait()
script.Parent.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame:lerp(script.Parent.HumanoidRootPart.CFrame + script.Parent.HumanoidRootPart.CFrame.lookVector*1, 0.5)
until forward.Value == false
end
end)
backward.Changed:Connect(function()
if backward.Value == true then
repeat
wait()
script.Parent.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame:lerp(script.Parent.HumanoidRootPart.CFrame + script.Parent.HumanoidRootPart.CFrame.lookVector*(-1), 0.5)
until backward.Value == false
end
end)
leftside.Changed:Connect(function()
if leftside.Value == true then
repeat
wait()
script.Parent.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame:lerp(script.Parent.HumanoidRootPart.CFrame + script.Parent.HumanoidRootPart.CFrame.rightVector*(-1), 0.5)
until leftside.Value == false
end
end)
rightside.Changed:Connect(function()
if rightside.Value == true then
repeat
wait()
script.Parent.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame:lerp(script.Parent.HumanoidRootPart.CFrame + script.Parent.HumanoidRootPart.CFrame.rightVector*1, 0.5)
until rightside.Value == false
end
end)
jump.Changed:Connect(function()
if jump.Value == true then
script.Parent.HumanoidRootPart.CFrame = script.Parent.HumanoidRootPart.CFrame:lerp(script.Parent.HumanoidRootPart.CFrame + script.Parent.HumanoidRootPart.CFrame.upVector*10, 1)
end
end)
-- check if the movements are activated or deactivated every time they change and move depending on the results