Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this momentum script lag so much when you hit max momentum?

Asked by 2 years ago

Ok so i'm making a movement system and I started first by adding momentum. But when you reach max momentum it starts lagging pretty severely.

Here's the code (It's in StarterCharacterScripts and is a local script)

local Plr = game.Players.LocalPlayer
local Char = Plr.Character
local Humanoid = Char:FindFirstChild("Humanoid")
local Sprinting = false
local UIS = game:GetService("UserInputService")
local RunKey = Enum.KeyCode.LeftShift
local Momentum = 0
local MaxMomentum = 1
local DefaultSpeed = 16

UIS.InputBegan:Connect(function(input,gpe)
    if gpe then return end

    if input.KeyCode == RunKey then
        Sprinting = true

        task.spawn(function()
            while Sprinting do
                task.wait()
                if Momentum >= MaxMomentum then
                    Momentum = MaxMomentum
                else
                    task.wait(0.5)
                    Momentum += 0.1
                end
            end
        end)

    end

end)

UIS.InputEnded:Connect(function(input,gpe)
    if gpe then return end

    if input.KeyCode == RunKey then
        Sprinting = false
        Momentum = 0
    end

end)


while wait() do

    if Sprinting then
        Humanoid.WalkSpeed = (DefaultSpeed*(Momentum+1))
    else
        Humanoid.WalkSpeed = DefaultSpeed
    end

end

If you can help that would be greatly appreciated :)

1 answer

Log in to vote
1
Answered by
xXMadonXx 190
2 years ago
Edited 2 years ago

Lagging with a WalkSpeed of 32? If you are in a modeled environment, your PC just can not seem to keep up (although 32 is still really low). Test it on only a Baseplate. You should easily be able to get speeds up to 1000 or more.

Enable the micro Profiler and watch your Framerate.

My current guess: Your PC is too slow.

Edit: The script itself is fine

0
Oh, once i test on a baseplate it works fine either my pc is too slow or it's another script in the game messing it up. Thanks! Littlebigplanet40000 77 — 2y
Ad

Answer this question