Walkspeed manager not stacking

I found this walkspeed manager module: https://create.roblox.com/store/asset/7440559028/WalkSpeedHandler

I modified it so that the speeds are based off of multiplication instead of addition, but the thing is is that walkspeed modifiers do not stack, for example, if I equip a tool that slows me down and then I sprint my walkspeed returns back to normal instead of slowing down. I cannot think of any solutions.

local walkSpeedHandler = {}
local playerModifiers = {}

function walkSpeedHandler.UpdateSpeed(player)
	local speed = 12
	local mods = playerModifiers[player]
	if mods then
		for modifierName, amount in pairs(mods) do
			speed *= amount
		end
	end
	if player.Character and player.Character:FindFirstChild("Humanoid") then
		player.Character.Humanoid.WalkSpeed = speed
	end
	return speed
end

function walkSpeedHandler.AddModifier(player, modifierName, amount)
	if not playerModifiers[player] then
		playerModifiers[player] = {}
	end

	playerModifiers[player][modifierName] = amount

	walkSpeedHandler.UpdateSpeed(player)
end

function walkSpeedHandler.RemoveModifier(player,modifierName)
	if playerModifiers[player] then
		if playerModifiers[player][modifierName] then
			playerModifiers[player][modifierName] = nil
		end
	end
	
	walkSpeedHandler.UpdateSpeed(player)
end

function walkSpeedHandler.ClearModifiers(player)
	if playerModifiers[player] then
		table.clear(playerModifiers[player])
	end
	
	walkSpeedHandler.UpdateSpeed(player)
end



return walkSpeedHandler
2 Likes

The logic for multiplying looks fine in UpdateSpeed, so the issue is likely how you are calling AddModifier or RemoveModifier from your tools. If a tool script is overwriting the same modifier name instead of using a unique one, it will just replace the previous value rather than stacking. Check if your sprinting logic and your tool logic are both trying to use the same key in the playerModifiers table.

1 Like

The modifiers all have different names. I’m also pretty sure its the speed variable at the top of UpdateSpeed. I’ve printed the speed before and after the multiplication, and the speed before is always 12, but I’m not sure how to change the speed without exponentially increasing it.

how sprint works? does it add modifier or changes humanoid’s spees directly?

My sprint system adds a modifier

what values do you use for the “tool that slows me down” and “sprint”?

WalkSpeedHandler.AddModifier(LocalPlayer, "Sprint", 1.67)
WalkSpeedHandler.AddModifier(Player, "Sword", 0.5)

If it helps, the sword modifier is called in a server script while the sprint modifier is called in a client script

hold on,
server and client are separated, they have different instances of the WalkSpeedHandler
so last one who call UpdateSpeed speed wins.
add print with the speed before

player.Character.Humanoid.WalkSpeed = speed

and check printed value.

Is this what you mean?

I’m positive this issue is due to the fact that UpdateSpeed has the walkspeed stuck at 12 at the top of the function, and adding modifiers leads to 12 being modified by whatever, instead of stacking, I just don’t know a solution

i am confused:

  • 10.02 is both sprint and the sword; .5 * 1.67 * 12
  • 20.04 is sprint; 1.67 * 12

what makes 6.36 and 10.6212?

Oh yeah 6.36 is crouching, and I think 10.6212 is also something to do with crouch

not sure what do you mean by “instead of stacking”
but current implementation

stacks correctly, as we can see in the logs:

  • 10.02 is both sprint and the sword; .5 * 1.67 * 12
  • 20.04 is sprint only; 1.67 * 12

so, sword slows down twice, the same as sprint multiplies speed by 1.67

instead of hardcoding 12 in the method you can set StarterPlayer’s CharacterWalkSpeed.


and get it in the code.

1 Like

I’ve discovered that 10.6212 is the speed that happens when I crouched, equip my sword, and then sprint.
By stacking I mean, I sprint which is 20 walkspeed, and then I equip my sword which causes sprinting and non-sprinting walkspeed to be half, or vice versa,
12 * 1.67 * 0.5

But currently the next modifier overrides it, like if I equip my sword, then sprint, sprint is at 20

add print with modifierName, before
speed *= amount
so you can see current modifers

Based off of the printing, only one modifier seems to be running at a time?
image

have you tried using sprint?
it still prints only sword?

Sorry, could you clarify further? I don’t understand what you mean

i mean when you have sword and toggle sprint
then there are must be two mods: sword and sprint
and speed should be 10.02 because .5 * 1.67 * 12

this is what you are about to check with logs:

  • there are two mods
  • and speed is 10.02

if this is not as expected then it is a starting point to investigate further, for example, possible out comes:

  • only one sprint mod, then the question is “where’s is the sword mod?”
  • speed does not match mods - “what’s wrong with math?”
  • etc

image
this is what I was talking about

2 Likes