Script is only reducing stamina once

Im trying to make a sprint system for my game, but it is only reducing stamina once every time the event fires.
It used too not reduce at all, but i changed it from “char:FindFirstChildOfClass(“Humanoid”).Running = true” to "char:FindFirstChildOfClass(“Humanoid”).MoveDirection.Magnitude > 0 " and now it reduces only once per sprint, help plz!

task.wait(1)
local char = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Character
local stamina = script.Parent.Parent.Stamina

while task.wait(0.1) do
	script.Parent.Events.start.OnServerEvent:Wait()
	char:FindFirstChildOfClass("Humanoid").WalkSpeed += 13
	repeat 
		task.wait(0.1)
		if char:FindFirstChildOfClass("Humanoid").MoveDirection.Magnitude > 0  then
			stamina.Value = stamina.Value - 1
		end
	until script.Parent.Events.enda.OnServerEvent:Wait() or stamina.Value == 0
	char:FindFirstChildOfClass("Humanoid").WalkSpeed -= 13

end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

script.Parent.Events.enda.OnServerEvent:Wait() doesn’t work as you seem to assume it does. Calling :Wait() on a script signal will yield until it fires, not let you know if it’s been fired. I assume you mean to do something like this:

task.wait(1)
local char = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Character
local stamina = script.Parent.Parent.Stamina

while task.wait(0.1) do
	script.Parent.Events.start.OnServerEvent:Wait()
	char:FindFirstChildOfClass("Humanoid").WalkSpeed += 13
	
	local hasStoppedSprinting = false
	local stoppedConnection = script.Parent.Events.enda.OnServerEvent:Connect(function()
		hasStoppedSprinting = true
	end)
	
	repeat 
		task.wait(0.1)
		if char:FindFirstChildOfClass("Humanoid").MoveDirection.Magnitude > 0  then
			stamina.Value = stamina.Value - 1
		end
	until hasStoppedSprinting or stamina.Value == 0
	
	char:FindFirstChildOfClass("Humanoid").WalkSpeed -= 13
	stoppedConnection:Disconnect() 
    --// Always make sure you dispose of temporary connections like these, 
    --// they can slow your game down or cause memory leaks
end

Now your loop will wait until the character starts sprinting, then uses a function to listen for when they stop sprinting which sets a variable to true if and when it happens. Then the repeat loop can check that variable and terminate if it’s true.

1 Like

tysm!!! this works thanks

1 Like

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