I’ve been searching through the forum for some solutions on how to effectively manage a player’s walkspeed, because let’s say in a game, the player can sprint, increasing walkspeed, and also use abilities, either slowing the player down or increasing the player’s speed, or when the player gets stunned, setting the walkspeed to 0 temporarily. There will be times when they overlap and then the walkspeed will be at a different value/wrong value. What is the best way to manage this system? Maybe like a queue system?
Example:
Let’s say the player’s default walkspeed is 16
Now the player gets stunned for 3 seconds. For three seconds their walkspeed will be 0
but before it gets to 3 seconds the player gets hit with an ability that increases everybodys speed by 10. Now his walkspeed is 10, but he still shouldn’t be able to move. After he is unstunned his walkspeed is set back to normal, but with the 10+ walkspeed boost added, so after hes unstunned his walkspeed will 26.
You can try passing the type, i.e. “stun” where nothing else for the duration will be applied, or a “boost” or a “change”, where “change” will be added to the current “boost”, you can experiment and fit it to your desired!
A function, or a module to manage all of this may just be enough for your purposes.
local WalkSpeedHandler = {}
setmetatable({}, WalkSpeedHandler)
WalkSpeedHandler.Character = game:GetService("Players").LocalPlayer.Character
WalkSpeedHandler.WalkSpeed = WalkSpeedHandler.Character:FindFirstChildOfClass("Humanoid").WalkSpeed
function WalkSpeedHandler:Stun()
self.WalkSpeed = 0
end
function WalkSpeedHandler:UnStun(Walkspeed)
if self.Character:GetAttribute("Boost") then
self.WalkSpeed = game.StarterPlayer.CharacterWalkSpeed + self.Character:GetAttribute("Boost")
else
self.WalkSpeed = Walkspeed or game.StarterPlayer.CharacterWalkSpeed
end
end
return WalkSpeedHandler
You can try my walkspeed handler module! You can add modifiers to lower / increase speed (by a flat amount, not using multiplying) and update it. Should be fairly easy to understand but let me know if you have any issues!
I’ve solved this problem for a while now I’m pretty sure. If you’re still looking for a solution: what I did was essentially have a module that stores all the walkspeed changes of each player. Depending on what type of change they get (adding/subtracting, or multiplying/dividing), you can set your own priority to however you like. If anyone has questions feel free to message me