I’ve got this bit of code that’s meant to cap a player’s speed at a max value. The problem is, there’s this persistent bug I keep running into—sometimes after a player rebirths, their speed doesn’t reset back to 1. Instead, it just stays stuck at 60.
I’ve tried everything I could think of to fix it. I’ve dug through the code, rewrote parts of it, and even asked AI for help. And just when I thought I finally had it solved… boom, the bug came right back.
If anyone has any ideas on what might be causing it, I’d really appreciate the help. It’s been driving me crazy. Here is the code:
local MAX_SPEED = 60
local player = game.Players.LocalPlayer
local event = rs.RemoteEvents:WaitForChild("ResetSpeed")
local function updateSpeed()
while true do
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
local stats = player:FindFirstChild("leaderstats")
local speed = stats and stats:FindFirstChild("Speed")
if humanoid and speed then
local finalSpeed = speed.Value + 16
humanoid.WalkSpeed = finalSpeed
if speed.Value >= MAX_SPEED then
event:FireServer(MAX_SPEED)
player:SetAttribute("SpeedMax", true)
else
player:SetAttribute("SpeedMax", false)
end
end
wait(0.1)
end
end
task.spawn(updateSpeed)
This is a local script. Thank you, have a nice day!
So the event remote was to put the speed back at 60 if they happen to spam the button to fast and get more than 60.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local rebirthEvent = ReplicatedStorage.RemoteEvents:WaitForChild("RebirthRequest")
-- Function to calculate dynamic rebirth cost
local function getRebirthCost(currentRebirths)
return math.floor(1000 * (1.75 ^ currentRebirths))
end
rebirthEvent.OnServerEvent:Connect(function(player)
local stats = player:FindFirstChild("leaderstats")
if not stats then return end
local cash = stats:FindFirstChild("Cash")
local rebirths = stats:FindFirstChild("Rebirths")
local speed = stats:FindFirstChild("Speed")
local delivery = stats:FindFirstChild("CashPerDelivery")
if not cash or not rebirths or not speed or not delivery then return end
local cost = getRebirthCost(rebirths.Value)
if cash.Value < cost then
-- Not enough money
return
end
-- Deduct money
cash.Value = 0
-- Reset upgrade stats
print("Before Rebirth: Speed =", speed.Value)
speed.Value = 1
print("After Rebirth: Speed =", speed.Value)
delivery.Value = 1
-- Increase rebirth count
rebirths.Value += 1
-- No passive upgrade applied directly — upgrades will scale better thanks to rebirths
end)
But I also have this code that handles the rebirths, if that helps.
I’m not exactly sure how to fix this just yet but I figured it might be that your script is conflicting with each other. So try checking the order at which the functions are run. Run print commands at the parts where it changes the speedvalue to see which one is overriding which one.
I just realised you’re running a while true do loop. This might be the reason why it stays 60. While the server already switched it to 1, the client hasn’t registered the change yet so it still sends out a signal to change it to 60. Use some sort of debounce to prevent sending more than it needs to.
There’s also an alternative where you just increase the time between checking but that’s if you’re willing to sacrifice speed.