hi so i like got my stamina system done and going, but heres the thing - i dont know how to really make sure the walkspeed stays consistent, so like, if its 16, and the player sprints, it increases by a bit, to for example, 20, and then if theres a status effect that boosts the speed, it stacks with it.
kinda like so:
16
20 - sprint
25 - sprint and status effect
21 - status effect
problem is i dont really know how to do that, aside from maybe saving the player’s walkspeed to a number value?
-- // SERVICES
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- // DEPENDENCY
local RemoteEvents = ReplicatedStorage.RemoteEvents
local PlayerStaminaEvent = RemoteEvents:WaitForChild("StaminaAction", 7)
-- // MODULE
local StaminaModule = {}
StaminaModule.ActivePlayers = {}
StaminaModule.UpdateLoop = nil
-- // CONFIGURATION <<<
local LOOP_RATE = 1/4
local REGEN_DELAY = 2 -- seconds
local REGEN_RATE = .25 -- per loop
local DRAIN_RATE = .15 -- per loop
local BASE_SPRINT_SPEED = 20
------------------------->>>
local function getSprintSpeed(humanoid)
local stat = humanoid:FindFirstChild("Stats")
if stat then
local sprintSpeed = stat:GetAttribute("SprintSpeed")
if sprintSpeed then
return sprintSpeed
end
end
return BASE_SPRINT_SPEED
end
function StaminaModule:SetRunning(player, state)
local data = self.ActivePlayers[player]
if data then
data.IsRunning = state
end
end
function StaminaModule:GetStamina(player)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
return humanoid:GetAttribute("Stamina") or 0
end
function StaminaModule:SetMaxStamina(player, amount)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
humanoid:SetAttribute("MaxStamina", amount)
end
function StaminaModule:SetStamina(player, amount)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
humanoid:SetAttribute("Stamina", amount)
end
function StaminaModule:AdjustMaxStamina(player, amount)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
local maxStamina = humanoid:GetAttribute("MaxStamina")
if maxStamina then
humanoid:SetAttribute("MaxStamina", maxStamina + amount)
end
end
function StaminaModule:AdjustStamina(player, amount)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
local max= humanoid:GetAttribute("MaxStamina") or 100
humanoid:SetAttribute("Stamina",math.clamp((humanoid:GetAttribute("Stamina") or max)+amount,0,max))
end
function StaminaModule:Initialize()
if self.UpdateLoop then self.UpdateLoop:Disconnect() end
StaminaModule.UpdateLoop = RunService.Heartbeat:Connect(function()
for player, data in next, self.ActivePlayers do
local character = player.Character :: Model
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health <= 0 then continue end
if player:GetAttribute("Menu") == true then continue end
local currentTime = tick()
local baseSpeed = getSprintSpeed(humanoid)
if data.IsRunning then
self:AdjustStamina(player, -DRAIN_RATE)
data.LastWalkSpeed = humanoid.WalkSpeed
humanoid.WalkSpeed = humanoid.WalkSpeed + baseSpeed
data.LastDrainTime = currentTime
else
if currentTime - (data.LastDrainTime or 0) >= REGEN_DELAY then
self:AdjustStamina(player, REGEN_RATE)
humanoid.WalkSpeed = humanoid.WalkSpeed - baseSpeed
end
end
print(player.Name, self:GetStamina(player))
end
task.wait(LOOP_RATE)
end)
end
local function OnPlayerStaminaEvent(plr,data)
if typeof(data)=="table" and data.Action~=nil then
StaminaModule:SetRunning(plr,data.Action)
end
end
local function OnPlayerJoin(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid") :: Humanoid
StaminaModule.ActivePlayers[plr]={
IsRunning=false,
LastDrainTime = 0,
LastWalkSpeed = hum.WalkSpeed
}
end
local function OnPlayerLeave(plr)
StaminaModule.ActivePlayers[plr]=nil
end
Players.PlayerAdded:Connect(OnPlayerJoin)
Players.PlayerRemoving:Connect(OnPlayerLeave)
PlayerStaminaEvent.OnServerEvent:Connect(OnPlayerStaminaEvent)
return StaminaModule
as you can see i tried adding it and subtracting it like that but it dont workk for obvious reasons
please disregard the code other than Initialize and other necessary functions. thank you