My Sprint Script isnt working and i have no idea why

Hello :wave:
Im honest i literly thinking im losing my mind here in this script i had the problem that it keeps rising in speed instead of staying one value and i thought that when it calculates
SpeedWalk = BaseWalk * SprintData.SpeedIncreaseDecrease
SpeedRun = BaseSprint * SprintData.SpeedIncreaseDecrease

that the value or result stays like example 3 and it only changes when a server event fires waht it currently dosent because no buffs are getting aplied or stun and when it sets it it should constently sets it to 3 but for some reson it increases indefenetly until im the flash and im at a loss currently having no idea why it isnt working can some one explain if i got it wrong or somthing and when yes help me with it

thank you :saluting_face:

local Cas = game:GetService("ContextActionService")
local RNS = game:GetService("RunService")

local StaminaEvent = game.ReplicatedStorage.RemoteEvents.StaminaRemover
local GotStunned = game.ReplicatedStorage.RemoteEvents.GotStunned

local UpdateStats = game.ReplicatedStorage.RemoteEvents.UpdateStats
local SprintData = require(script.Parent:WaitForChild("CharacterStats"))-- Your module script here
local isStunned = false

local BaseWalk = SprintData.BaseWalkSpeed
local BaseSprint = SprintData.SprintSpeed

local SpeedWalk
local SpeedRun

SpeedWalk =  BaseWalk * SprintData.SpeedIncreaseDecrease
SpeedRun = BaseSprint * SprintData.SpeedIncreaseDecrease




local hasToRecharge = false


local function clampStamina()
	SprintData.Stamina = math.clamp(SprintData.Stamina, 0, SprintData.MaxStamina)
end

StaminaEvent.OnClientEvent:Connect(function(newStamina)
	print("Stamina updated:", newStamina)
	hasToRecharge = true
	SprintData.Stamina -= newStamina -- update local value or UI bar
	clampStamina()
end)

UpdateStats.OnClientEvent:Connect(function(newStats)
	for key, value in pairs(newStats) do
		SprintData[key] = value
	end
	
	SpeedWalk = BaseWalk * SprintData.SpeedIncreaseDecrease
	SpeedRun = BaseSprint * SprintData.SpeedIncreaseDecrease
end)


local staminausage = 0.2
local stamianrecharg = 0.25
local rechargeTime = 0.25

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

local RootPart = char:WaitForChild("HumanoidRootPart")
local RunSound = RootPart:WaitForChild("Running")
local sprint = script.RunAnim
local Runanim = humanoid.Animator:LoadAnimation(sprint)

local lastSprint = math.huge

local isSprinting = false

humanoid.JumpPower = 40

local jumpCooldown = 1
local canJump = true

GotStunned.OnClientEvent:Connect(function(duration)
	isStunned = true
	isSprinting = false

	local oldBaseWalk = SprintData.BaseWalkSpeed
	local oldSprint = SprintData.SprintSpeed

	-- Prevent movement
	SprintData.BaseWalkSpeed = 0
	SprintData.SprintSpeed = 0
	humanoid.WalkSpeed = 0
	Runanim:Stop()

	task.delay(duration, function()
		isStunned = false
		SprintData.BaseWalkSpeed = oldBaseWalk
		SprintData.SprintSpeed = oldSprint
		humanoid.WalkSpeed = oldBaseWalk * SprintData.SpeedIncreaseDecrease
	end)
end)

humanoid.StateChanged:Connect(function(_, newstate)
	if newstate == Enum.HumanoidStateType.Jumping and canJump then
		isSprinting = false
		Runanim:Stop()
		humanoid.WalkSpeed = SpeedWalk
		RunSound.PlaybackSpeed = 1

		canJump = false
		humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) 

		task.delay(jumpCooldown, function()
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true) 
			canJump = true
		end)
	end
end)

local function sprint(actionname, inputstate, inputtyp)
	if isStunned then
		return
	end
	print("SprintSpeed:", SprintData.SprintSpeed, "Multiplier:", SprintData.SpeedIncreaseDecrease)

	if inputstate == Enum.UserInputState.Begin then
		isSprinting = true
	elseif inputstate == Enum.UserInputState.End then
		isSprinting = false
	end

	if isSprinting then

		humanoid.WalkSpeed = SpeedRun


		RunSound.PlaybackSpeed = 1.25
		if humanoid.MoveDirection.Magnitude > 0 then
			Runanim:Play()
		end
	else
		humanoid.WalkSpeed = SpeedWalk
		RunSound.PlaybackSpeed = 1
		Runanim:Stop()
	end
end

Cas:BindAction("Sprint", sprint, true, Enum.KeyCode.LeftShift)

RNS.Heartbeat:Connect(function(delta)
	local stamina = SprintData.Stamina

	if not isSprinting then
		humanoid.WalkSpeed = SpeedWalk
	else
		-- If sprinting, apply SprintSpeed with SpeedIncreaseDecrease
		humanoid.WalkSpeed = SpeedRun
	end


	if stamina < staminausage then
		isSprinting = false
		RunSound.PlaybackSpeed = 1
		Runanim:Stop()
		humanoid.WalkSpeed = SpeedWalk
	end
	if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
		SprintData.Stamina = stamina - staminausage  -- Reduce stamina using the module
		lastSprint = tick()
	else
		if tick() - lastSprint >= rechargeTime and SprintData.Stamina < SprintData.MaxStamina or hasToRecharge == true then
			SprintData.Stamina = SprintData.Stamina + stamianrecharg
			clampStamina()

			if SprintData.Stamina >= SprintData.MaxStamina then
				hasToRecharge = false
			end
		end
	end
end)

Note do to another script that literly shoots the event it resets it and prevents infenetly incresments proving that if the SpeedIncreaseDecrease is 1 insted of any other value like now 1.1(10% increase) it works all fine im not joking it works fine with no buff but when it gets one it breaks logic and like i set the reset makes it weird like you randomly speed up and slow down exept i trun the script off then i get indefenetly fast but it seems like it cause its because of the Chraachter script or the buff that this charachter ahs waht is movment speed if any debuff or buff apears it dosent do it

1 Like

Your math isn’t the issue; multiplying by the speed multiplier won’t increase the value over time on its own. If your walk/run speed keeps rising, it means one of the values you’re multiplying is being modified elsewhere.

In your script, this line is the key:

for key, value in pairs(newStats) do
    SprintData[key] = value
end

If the server fires this remote more than once, or another script edits SprintData.BaseWalkSpeed, SprintData.SprintSpeed, or SprintData.SpeedIncreaseDecrease, then the client ends up recalculating speed using already modified values, which causes the compounding effect you’re seeing.

This also explains why everything behaves normally when the multiplier is 1. Multiplying by 1 doesn’t compound, so the bug is hidden.

Simple fix:
Always calculate speeds using the original base values, not the ones inside SprintData after they’ve been changed.

Example:

local BaseWalk = SprintData.BaseWalkSpeed
local BaseSprint = SprintData.SprintSpeed

local function updateSpeeds()
    SpeedWalk = BaseWalk * SprintData.SpeedIncreaseDecrease
    SpeedRun  = BaseSprint * SprintData.SpeedIncreaseDecrease
end

And call updateSpeeds() whenever stats change.

This keeps the values stable and prevents exponential growth.

3 Likes

First of all thanks to your replie i thougt about it and realised that it cloudnt be possible for it to constatly fire and i spent 30 min serching through all my scripts and turned outs one of the scripts that debuffed the player did this:

while task.wait(1) do
	if Picture.Visible  then
		buff()
	else
		Stats:FireServer(0.60,nil,nil,nil,nil,nil)
	end
end

if the picture was isnvisibel (debuff) then it buff him indefenetly i fixed it:

while task.wait(1) do
	if Picture.Visible and debounce then
		buff()
	elseif not Picture.Visible and not debounce then
		debounce = true
		Stats:FireServer(0.60,nil,nil,nil,nil,nil)
	end
end

and now it finnaly works if you didnt pointed out that if it fires to often i wouldve never realised that and never searched for that Rouge Script

Thank You for your Help :saluting_face:

2 Likes

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