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
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.
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.
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
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’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