Weird Execution Issue

-- these 3 lines will be wrapped in a event
print("HELLO")
task.wait(9.5)
print("Hello AGain")

while true do
-- do stuff
task.wait()
end

so my script is sorta like this and it prints “HELLO” but never prints hello again could it be because the executer is only focused on the task.wait() loop? i dont know why it doesnt print hello again. this is local script btw.
For my solution, well to see what happens, I did this:

		print("its true")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(1)
		print("yes")
		task.wait(.5)
		print("yes")
		print("i am doing it")

It prints yes 3 times and then just randomly stops… My event is a .changed event for a bool value. Im pretty sure it does not switch while the task.wait(9.5) stuff is happening.

I did some more testing, and it seems that it arbitrarily stops. Sometimes it stops when it prints 3 yes’, sometimes it stopes when it prints 5, etc.

Paste the whole code. Nobody can help you without context.

local player = game.Players.LocalPlayer
local userInput = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local round = game.ReplicatedStorage.values.round
local ContextActionService = game:GetService"ContextActionService"

local stamina = 0
local canRun = false
local running = false
local canSprint = true
local canRegen = true

math.clamp(stamina, 0, 90.7)

local tweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Linear			
)

local function remove(obj)
	task.wait(15)
	obj:Destroy()
end

local function sight()
	for _, v in next, game.Players:GetPlayers() do
		local plr = v
		if v.Character then
			if plr.Character:FindFirstChild("survivor") then
				local sight = game.ReplicatedStorage.models.benSight:Clone()
				sight.Parent = plr.Character.HumanoidRootPart
				task.spawn(remove, sight)
			end
		end
	end
end

local bar = player.PlayerGui:WaitForChild("sightGui").background.Frame

local function buttonPressed(name, state, input)
	local char = player.Character
	local bar = player.PlayerGui:WaitForChild("sightGui").background.Frame
	if state == Enum.UserInputState.Begin and canSprint == true and game.ReplicatedStorage.values.round.Value == true and char.HumanoidRootPart:FindFirstChild("IAmMonster") and not char:FindFirstChild("survivor") then
		canRun = true
		running = true
		while stamina >= 0 and running == true do
			if stamina >= 90.7 then
				game.ReplicatedStorage.events.screamEvent:FireServer()
				task.spawn(sight)
				stamina -= 90.7
			end
			if stamina <= 75 and stamina > 50 then
				canSprint = true
				local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
				tweenService:Create(bar, tweenInfo, Goal):Play()
			end
			if stamina <= 50 and stamina > 25 then
				canSprint = false
				local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
				tweenService:Create(bar, tweenInfo, Goal):Play()
			end
			if stamina <= 25 then
				canSprint = false
				local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
				tweenService:Create(bar, tweenInfo, Goal):Play()
			end
			bar:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
			task.wait()		
		end
	else
		if game.ReplicatedStorage.values.round.Value == true and char.HumanoidRootPart:FindFirstChild("IAmMonster") and not char:FindFirstChild("survivor")  then
			running = false
			if canRegen == true then
				while stamina < 90.7 and not running do
					stamina += .02
					canRegen = false
					if stamina >= 75 then
						canSprint = true
						local Goal = {BackgroundColor3 = Color3.fromRGB(0, 160, 0)}
						tweenService:Create(bar, tweenInfo, Goal):Play()
					end
					if stamina <= 75 and stamina > 50 then
						canSprint = true
						local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
						tweenService:Create(bar, tweenInfo, Goal):Play()
					end
					if stamina <= 50 and stamina > 25 then
						canSprint = false
						local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
						tweenService:Create(bar, tweenInfo, Goal):Play()
					end
					if stamina <= 25 then
						canSprint = false
						local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
						tweenService:Create(bar, tweenInfo, Goal):Play()
					end
					bar:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
					task.wait()
				end
			end
			canRegen = true
		end
	end
	return Enum.ContextActionResult.Pass
end

function placeButton()
	print("i hope i work")
	task.wait(9.5)
	local char = player.Character or player.CharacterAdded:Wait()
	if char:WaitForChild("HumanoidRootPart"):FindFirstChild("IAmMonster") then
		print("i worked")
		ContextActionService:SetPosition("sightButton",UDim2.new(0.72,-20,0.20,-20))
		ContextActionService:SetTitle("sightButton","Sight")
	end
end


game.ReplicatedStorage.values.round.Changed:Connect(function()
	local char
	local sprintButton = ContextActionService:BindAction("sightButton", buttonPressed, true)
	ContextActionService:SetPosition("sightButton",UDim2.new(0,0,500,0))
	ContextActionService:SetTitle("sightButton","Sight")
	if game.ReplicatedStorage.values.round.Value == true then
		print("i am true")
		task.wait(9.5)
		print("oml")
		char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
		if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild("IAmMonster") then
			print("i worked")
			ContextActionService:SetPosition("sightButton",UDim2.new(0.72,-20,0.20,-20))
			ContextActionService:SetTitle("sightButton","Sight")
		end
	end
	if game.ReplicatedStorage.values.round.Value == false then
		print("now im false???")
		ContextActionService:UnbindAction("sightButton")
		ContextActionService:SetPosition("sightButton",UDim2.new(0,0,500,0))
		ContextActionService:SetTitle("sightButton","Sight")
	end
end)


script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
if stamina <= 25 then
	canSprint = false
	local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
	tweenService:Create(script.Parent, tweenInfo, Goal):Play()
end
running = false
if canRegen == true then
	while stamina < 90.7 and not running do
		stamina += .02
		canRegen = false
		if stamina >= 75 then
			canSprint = true
			local Goal = {BackgroundColor3 = Color3.fromRGB(0, 160, 0)}
			tweenService:Create(script.Parent, tweenInfo, Goal):Play()
		end
		if stamina <= 75 and stamina > 50 then
			canSprint = true
			local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
			tweenService:Create(script.Parent, tweenInfo, Goal):Play()
		end
		if stamina <= 50 and stamina > 25 then
			canSprint = false
			local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
			tweenService:Create(script.Parent, tweenInfo, Goal):Play()
		end
		if stamina <= 25 then
			canSprint = false
			local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
			tweenService:Create(script.Parent, tweenInfo, Goal):Play()
		end
		script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
		task.wait()
	end
end
canRegen = true

userInput.InputBegan:Connect(function(input)
	local char = player.Character
	if input.KeyCode == Enum.KeyCode.E and canSprint == true and round.Value == true and not char:FindFirstChild("survivor") then
		canRun = true
		running = true
		while stamina >= 0 and running == true do
			if stamina >= 90.7 then
				game.ReplicatedStorage.events.screamEvent:FireServer()
				task.spawn(sight)
				stamina -= 90.7
			end
			if stamina <= 75 and stamina > 50 then
				canSprint = true
				local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
				tweenService:Create(script.Parent, tweenInfo, Goal):Play()
			end
			if stamina <= 50 and stamina > 25 then
				canSprint = false
				local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
				tweenService:Create(script.Parent, tweenInfo, Goal):Play()
			end
			if stamina <= 25 then
				canSprint = false
				local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
				tweenService:Create(script.Parent, tweenInfo, Goal):Play()
			end
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
			task.wait()
		end
	end
end) 

userInput.InputEnded:Connect(function(input)
	local char = player.Character
	if input.KeyCode == Enum.KeyCode.E and round.Value == true and char.HumanoidRootPart:FindFirstChild("IAmMonster") and not char:FindFirstChild("survivor") then
		running = false
		if canRegen == true then
			while stamina < 90.7 and not running do
				stamina += .02
				canRegen = false
				if stamina >= 75 then
					canSprint = true
					local Goal = {BackgroundColor3 = Color3.fromRGB(0, 160, 0)}
					tweenService:Create(script.Parent, tweenInfo, Goal):Play()
				end
				if stamina <= 75 and stamina > 50 then
					canSprint = true
					local Goal = {BackgroundColor3 = Color3.fromRGB(144, 156, 36)}
					tweenService:Create(script.Parent, tweenInfo, Goal):Play()
				end
				if stamina <= 50 and stamina > 25 then
					canSprint = false
					local Goal = {BackgroundColor3 = Color3.fromRGB(161, 99, 33)}
					tweenService:Create(script.Parent, tweenInfo, Goal):Play()
				end
				if stamina <= 25 then
					canSprint = false
					local Goal = {BackgroundColor3 = Color3.fromRGB(168, 0, 24)}
					tweenService:Create(script.Parent, tweenInfo, Goal):Play()
				end
				script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 0.7 ,0), "Out", "Linear", 0)
				task.wait()
			end
		end
		canRegen = true
	end
end)

it never prints “oml”… Only prints “I am true”

houfbiojehru2girj923urhu23rju9830rr2iu083r2u3r903ri029r-23i0r-923-ri-023r
its asking me for 30 more characters so that’s why I typed jibberish

Where is this script located? Could it be that something is destroying the script while its waiting?

its a local script. also there is no possible way the script is being deleted because the task.wait() loop is still running. Its located in starter player scripts.

What happens when you use wait(9.5) instead of task.wait(9.5)?

nothing changes. It still randomly stops and doesn’t follow through the entire line.

The thread is completely killed if the script (or should I say, LuaSourceContainer) is destroyed (not parented to nil, but Instance:Destroy()). It doesn’t matter if you have a loop or not

Exactly. If the script was really killed then the loop in the script shouldn’t be continuing to be iterated.

Can you please put a while loop at the end of your script? Make it print something out every second. It shouldn’t interfere with the script too much. If it stops printing it out during the task.wait() thing, then the script is being destroyed

e.g.

while wait(1) do
    print("Cycled")
end

edit: Is there already a loop that continues?

edit2: Apparently yes, there is a loop. Can you try replacing the 9.5 in the task loop with a whole number?

( @3DesignD)

yep, I put a print in there and its iterating

Honestly, just disregard what I said. it would make no difference

edit: Does this post here help at all: Task.wait() not working - #15 by SussyImpastaFromSus

To see if something is happening like it is in the link i posted above, it may be restarting the script completely.
Can you please try putting a print() statement at the very start of the script, so you can see if it restarts?

YES! it prints restart twice just a few seconds after it prints “its true”

well, actually once before it, and then a few second afterwards then another one after it prints “its true”. that’s what I meant by twice.

From reading the post, I think I know a solution that may work. give me a second…

your an absolute life saver dude. I should have done more research on it instead of just posting a forum.

Edit: btw it fixed now. that took a couple hours.

so in my game, when you spawn in as a monster I have to put a model in starter character really quickly and :load the character. I never knew that local scripts reset when the player dies. Really good knowledge for the road ahead. I haven’t encountered a grueling bug like this in quite a while.