I’m trying to make a robot that uses velocity to walk instead of the actual Humanoid:Move() function since that also caused stuttering, any ideas as to why it isn’t working?
I’ve tried tinkering with the velocity, still causes stuttering. Could it be something to do with the code itself?
Here’s the code. (It’s in a LocalScript so I don’t have to use RemoteEvents which I don’t trust, as they can be fired any time.)
local runvelocity = Instance.new("BodyVelocity")
runvelocity.P = 25
runvelocity.MaxForce = Vector3.new(100000,0,100000)
local running = false
local inputservice = game:GetService("UserInputService")
inputservice.InputBegan:Connect(function(key)
if script.Parent.spawned.Value == true then
if key.KeyCode == Enum.KeyCode.W then
if runvelocity.Parent ~= robot.PrimaryPart then
runvelocity.Parent = robot.PrimaryPart
end
running = true
runvelocity.Velocity= robot.PrimaryPart.CFrame.LookVector * 10
repeat
if runvelocity.Velocity ~= robot.PrimaryPart.CFrame.LookVector * 10 then
runvelocity.Velocity= robot.PrimaryPart.CFrame.LookVector * 10
end
wait(0.5)
until inputservice:IsKeyDown(Enum.KeyCode.W) == false
running = false
print("stop")
runvelocity.Velocity = Vector3.new(0,0,0)
elseif key.KeyCode == Enum.KeyCode.Space then
robot:FindFirstChild("Humanoid").Jump = true
elseif key.KeyCode == Enum.KeyCode.A and running == false then
robot.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(30), 0)
print("left")
elseif key.KeyCode == Enum.KeyCode.D and running== false then
robot.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(-30), 0)
elseif key.KeyCode == Enum.KeyCode.E then
robot.Swing:FireServer()
end
end
end
)
As you can see in the video, after a while of holding down W, the robot’s movement starts to stutter.
try this, this creates a function and event to control the movement, rather than using repeat loops. This is more reliable
local runservice = game:GetService('RunService')
inputservice.InputBegan:Connect(function(key)
if script.Parent.spawned.Value == true then
if key.KeyCode == Enum.KeyCode.W then
if running == false then
running = true
if runvelocity.Parent ~= robot.PrimaryPart then
runvelocity.Parent = robot.PrimaryPart
end
local updateconnection
local function update()
if running == false then
updateconnection:Disconnect()
runvelocity.Velocity = Vector3.new(0,0,0)
print("stop")
else
if runvelocity.Velocity ~= robot.PrimaryPart.CFrame.LookVector * 10 then
runvelocity.Velocity= robot.PrimaryPart.CFrame.LookVector * 10
end
end
end
updateconnection = runservice.Heartbeat:Connect(update)
end
elseif key.KeyCode == Enum.KeyCode.Space then
robot:FindFirstChild("Humanoid").Jump = true
elseif key.KeyCode == Enum.KeyCode.A and running == false then
robot.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(30), 0)
print("left")
elseif key.KeyCode == Enum.KeyCode.D and running== false then
robot.HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(-30), 0)
elseif key.KeyCode == Enum.KeyCode.E then
robot.Swing:FireServer()
end
end
end)
inputservice.InputEnded:connect(function(key)
if script.Parent.spawned.Value == true then
if key.KeyCode == Enum.KeyCode.W then
running = false
end
end
end)
try to remove the statement if runvelocity.Velocity ~= robot.PrimaryPart.CFrame.LookVector * 10 and just set the Velocity to robot.PrimaryPart.CFrame.LookVector * 10 without the if query