Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this cause a game script timeout whenever it's activated?

This is reposted btw

function module:Breathing(Player,Bool)
	local Character = Player.Character
	local plrData = game.ReplicatedStorage.PlayerData:FindFirstChild(Player.UserId)
	print(CooldownModule:IsOnCooldown(Player.Character,"Breathing"))
	if Bool and not CooldownModule:IsOnCooldown(Player.Character,"Breathing") and not CooldownModule:IsOnCooldown(Player.Character,"Active") then
		print(Bool)
		local Value = Instance.new("StringValue",Character)
		Value.Name = "Breathing"
		while Character:FindFirstChild("Breathing") and Bool do
			if plrData.Breath.Value < 100 then
			plrData.Breath.Value = plrData.Breath.Value + 1
			wait(0.1)
			end
		end
	elseif not Bool and Character:FindFirstChild("Breathing") then
		CooldownModule:CreateCooldown(Character,"Breathing",0.2)
		for i, v in pairs(Character:GetChildren()) do 
			if v.Name == "Breathing" then
				v:Destroy()
			end
		end
	end
end

2 comments

0

Which line does it say the timeout was on?

0

Also, you don't have to use 0.1. You can leave at as wait() and it will default to the smallest value possible.

1 answer

✓ Accepted answer

The wait function is inside the if statement code block. Here's a summary example:

local a=0
while true do--start loop
	if a==1 then-- since a==0 it skips this if statement
		wait(0.1);-- since the if statement is skipped, this wait function wont run, there's no cool down because while loops run really fast, thereby causing a timeout
	end;
end;

I suggest placing your wait function outside of the if statement, so that regardless of the condition, there will be a cool down to prevent timeout.

This discussion is preserved as it was. New answers, comments, and votes are closed.