Bug For Max Speed Cap

Hey everyone!

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!

-Asher

1 Like

could you send the server script that handles the .OnServerEvent() of the remote you fired so that other people could more easily help you out?

1 Like
local event = game.ReplicatedStorage.RemoteEvents:WaitForChild("ResetSpeed")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats

	local speedLevel = Instance.new("IntValue")
	speedLevel.Name = "Speed"
	speedLevel.Value = 1
	speedLevel.Parent = leaderstats

	local cashLevel = Instance.new("IntValue")
	cashLevel.Name = "CashPerDelivery"
	cashLevel.Value = 1
	cashLevel.Parent = leaderstats
	
	local rebirth = Instance.new("IntValue")
	rebirth.Name = "Rebirths"
	rebirth.Value = 0
	rebirth.Parent = leaderstats
	
	event.OnServerEvent:Connect(function(player, newSpeed)
		speedLevel.Value = newSpeed
	end)
end)

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.

1 Like

Ok I will try that. Thank you for your time!

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.