#speed resets when I stop moving

1 messages · Page 1 of 1 (latest)

glass vector
#

I have a level up system but when I stop moving the WalkSpeed immediately resets back to 16 and idk why

heres the module script that makes the Level Up system I have

local Data = {}
local Players = game:GetService("Players")
local player = Players.LocalPlayer

Data.Player_Data = {
    ["Speed"] = 16,
    ["Level"] = 1,
    ["Exp"] = 0,
    ["ExpNeeded"] = 100,
    ["Health"] = 100,
    ["MaxHealth"] = 100
}

function Data.CheckForLevelUp(Player_Data, HitPlayer)
    local Character = player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
    if not Character or not Humanoid then return end
    print("Checking for level up...")
    print("Current Level: " .. Data.Player_Data["Level"])
    if Data.Player_Data["Exp"] ~= Data.Player_Data["ExpNeeded"] then
        print("Exp Needed: " .. Data.Player_Data["ExpNeeded"])
    end
    if Data.Player_Data["Exp"] >= Data.Player_Data["ExpNeeded"] then
        print("Level Up!")
        Data.Player_Data["Level"] += 1
        Data.Player_Data["Exp"] = 0
        Data.Player_Data["ExpNeeded"] += 100
        print("New Level: " .. Data.Player_Data["Level"])
        print("New Exp: " .. Data.Player_Data["Exp"])
        print("New Exp Needed: " .. Data.Player_Data["ExpNeeded"])
        Humanoid.WalkSpeed = Data.Player_Data["Speed"] + 1
        print("New Speed: " .. Humanoid.WalkSpeed)
    end
    return Player_Data
end

function Data.gainExp(Player_Data, Amount)
        print("Exp Gained!")
            Data.Player_Data.Exp += 100
            Data.CheckForLevelUp(player, Player_Data)
    return Player_Data
end

return Data

#

and here is the local Script that makes the module script work

local ModScript = require(game.ReplicatedStorage.Modules.Data)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local part = workspace.Exp

part.Touched:Connect(function(hit)
    ModScript.gainExp(player)
    part.CanTouch = false
    part.Transparency = 1
    task.wait(2.5)
    part.CanTouch = true
    part.Transparency = 0
    task.wait(1.5)
end)
dry carbon
#

never use local scripts for this

stone glacier
#

Your Player_Data[“Speed”] value never actually updates, You‘re doing Humanoid.WalkSpeed = Data.Player_Data[“Speed”] + 1 which is always equal to 17 while Speed in your table remains 16 permanently. That would explain why every levelup always set it to 17 regardless of the level.

The real problem is that you‘re setting the WalkSpeed from a LocalScript. In a FilteringEnabled game (it‘s on by default), the server owns the character so the server will just override you and set the WalkSpeed themselves (hence it snapping back).

The fix is to use a RemoteEvent to tell the server to apply the speed change:

In a Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local speedEvent = ReplicatedStorage:WaitForChild("SetSpeed")

speedEvent.OnServerEvent:Connect(function(player, newSpeed)
    local char = player.Character
    if char then
        char:WaitForChild("Humanoid").WalkSpeed = newSpeed
    end
end)

Then in your module, fire it instead:

Data.Player_Data["Speed"] += 1  -- actually update the value!
local event = game:GetService("ReplicatedStorage"):WaitForChild("SetSpeed")
event:FireServer(Data.Player_Data["Speed"])

Firstly, you should make a new RemoteEvent named SetSpeed in ReplicatedStorage.

Also a side note never trust the client to do the exp/ level logic on a real game, since exploiters can just call gainExp() for themselves. Would probably be a good idea to do that on the server in the long run.