Need help with speed boosts

Hi. Im trying to implement a status effect system into my game, it currently only has the speed effect (basically a speed boost). How do i make the speed boost affect both walking and sprinting? It works on walking, but when you start sprinting it gets completely negated. I’ve been scratching my head over this for a while now, couldnt seem to find any similar posts. Help is greatly appreciated.

Here’s the stat effect module code:

local module = {
	["Speed"] = function(target:Model, stack, duration)
		if not target then print("no target found") return end
		
		local speedDiff = 4 + (stack * 2)
		
		target:WaitForChild("Humanoid").WalkSpeed += speedDiff
		task.wait(duration)
		target:WaitForChild("Humanoid").WalkSpeed -= speedDiff
		return
	end,
}

function module.ApplyEffect(effectName, target, stack, duration)
	if not effectName or not target then return end
	if not stack then stack = 1 end
	if not duration then duration = 1 end
	if type(target) == "table" then
		for i=1,#target do
			if target[i] then
				module.ApplyEffect(effectName, target[i], stack, duration)
			end
		end
	else
		if module[effectName] then
			spawn(function()
				module[effectName](target, stack, duration)
			end)
		end
	end
end

return module

And the sprint script code:

local Stamina = 110
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

local SprintHeld = false
local Sprinting = false
local Exhausted = false
local CantSprint = script.Parent:WaitForChild("cantSprint")

local RunRefresh = 20 
local SpeedDiff = 20.5
local DrainRate = 9.5 
local gainRate = 21 
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local UserInputService = game.UserInputService
local RunService = game["Run Service"]

script.Parent.cantSprint.Changed:Connect(function()
	SprintHeld = false
	Sprinting = false
end)

local function sprint(active)
	if Exhausted then return end 
	if CantSprint.Value then return end
	if active then
		game["Run Service"].RenderStepped:Wait()
		Humanoid.WalkSpeed = 28
	else
		game["Run Service"].RenderStepped:Wait()
		Humanoid.WalkSpeed = 7.5
	end
	Sprinting = active
end

UserInputService.InputBegan:Connect(function(input)
	if CantSprint.Value then return end
	if input.KeyCode ~= Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.ButtonR2 then return end
	SprintHeld = true
	sprint(SprintHeld)
end)

UserInputService.InputEnded:Connect(function(input)
	if CantSprint.Value then return end
	if input.KeyCode ~= Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.ButtonR2 then return end
	SprintHeld = false
	sprint(SprintHeld)
end)

RunService.Heartbeat:Connect(function(DeltaTime)
	game.Players.LocalPlayer.PlayerGui.ScreenGui.TextLabel.Text = "stamina: "..math.round(Stamina)
	if Sprinting and Humanoid.MoveDirection ~= Vector3.zero then
		if Stamina > 0 then
			Stamina = Stamina - DrainRate * DeltaTime
		else
			sprint(false)
			Exhausted = true
		end
	elseif Stamina < 110 then
		Stamina = Stamina + gainRate * DeltaTime
		if Stamina > RunRefresh then 
			Exhausted = false
			if SprintHeld then 
				sprint(SprintHeld)
			end
		end
	end
end)
1 Like

The sprint script overrides the player’s walkspeed by setting it to a specific value everytime it’s triggered
You should check if there’s an active speed boost in the sprinting script as well

You can take a look at the code in my module if you’re still confused

2 Likes