Best way to manage walkspeed

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.

2 Likes

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.

Hmmm, could you give some example code to this?

Short Answer: Module Scripts or/and Tag’s and Attributes

Yes, obviously, but I’m not sure i’m not sure I get what TripTimeKing is saying.

Honestly, me neither, if i actually try to read that i’ll probably have a stroke.

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!

2 Likes

Yeah sure I will check it out!

Does this handle overlapping walkspeeds

You can treat the walkspeed as a recipe.

When we start holding sprint, we’ll add 4.

When we get hit by slow, we’ll remove 10

WalkSpeed = {} -- 16

WalkSpeed = {4} -- 20

WalkSpeed = {4,-10} -- 10

To get the walkspeed you just add up all the elements.

5 Likes

This is pretty useful, Thank you!

1 Like

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

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