Wait() Stops the script for no reason

I’m trying to make a medkit script that heals… the user maybe?

This local script here is in the tool, it captures the player’s mouse, listen for key press events and fire remotes to a serverside to heal the user. But, the wait’s in this script just stops it. Here is a section of the code I’m having isuues with:

local function halt(a)
	if db then print'debounce' else
		db = true
		if a == "cancel" then print("cancelled")
			playing = false	
			cam.CameraSubject = char.Humanoid
			cam.CameraType = Enum.CameraType.Custom
			cam.CFrame = char.Head.CFrame
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Visible = false
			plr.PlayerGui.headsupdisplay.weapons.charge.Visible = false
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Position = UDim2.new(0.02, 0, 0.182, 0)
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Size = UDim2.new(0,0,0,7)
			db = false

		else print("not cancelled, nice.")
			plr.PlayerGui.headsupdisplay.helth.Visible = true
			script.Parent.RemoteEvent:FireServer()
			playing = false
			cam.CameraSubject = char.Humanoid
			cam.CameraType = Enum.CameraType.Custom
			cam.CFrame = char.Head.CFrame
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Visible = false
			plr.PlayerGui.headsupdisplay.weapons.charge.Visible = false
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Position = UDim2.new(0.02, 0, 0.182, 0)
			plr.PlayerGui.headsupdisplay.weapons.charge.bar.Size = UDim2.new(0,0,0,7)
			ts:Create(plr.PlayerGui.headsupdisplay.helth.move, TweenInfo.new(30, Enum.EasingStyle.Linear), {--[[Position = UDim2.new(0.05, 0, 0.04, 0),]] Size = UDim2.new(0.913, 0, 0, 0)}):Play()
			print("a")
			wait(30)
			print("aa")
			ts:Create(plr.PlayerGui.headsupdisplay.helth, TweenInfo.new(2), {BackgroundTransparency = 1}):Play()
			ts:Create(plr.PlayerGui.headsupdisplay.helth.move, TweenInfo.new(2), {BackgroundTransparency = 1}):Play()
			ts:Create(plr.PlayerGui.headsupdisplay.helth.ImageLabel, TweenInfo.new(2), {ImageTransparency = 1}):Play()
			print("aaa")
			wait(2)
			print("aaaa")
			plr.PlayerGui.headsupdisplay.helth.Visible = false

		end
		db = false
	end
end

(sorry, it is a spagetti, i know)

Yes, the first tween(ts:Create(plr.PlayerGui.headsupdisplay.helth.move, TweenInfo.new(30, Enum.EasingStyle.Linear), {Size = UDim2.new(0.913, 0, 0, 0)}):Play() ) works. The event fires too. Everything except below the wait(30) works.

I also tried tween.Completed:Wait(), it also stops the script.

After waiting for 30 seconds(and more), the output only shows “a” as an output. The others never show up. Any help for this? Thanks in advance.

You already tried using “task.wait(30)” instead of “wait(30)”?
Its rare to happend but sometimes the “wait” crash idk why

1 Like

Using wait() in a function will stop an entire script (most of the time) unless a value is returned. You can prevent this by using spawn().

spawn(function()
	local function halt(a)
		if db then print'debounce' else
			db = true
			if a == "cancel" then print("cancelled")
				playing = false	
				cam.CameraSubject = char.Humanoid
				cam.CameraType = Enum.CameraType.Custom
				cam.CFrame = char.Head.CFrame
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Visible = false
				plr.PlayerGui.headsupdisplay.weapons.charge.Visible = false
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Position = UDim2.new(0.02, 0, 0.182, 0)
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Size = UDim2.new(0,0,0,7)
				db = false

			else print("not cancelled, nice.")
				plr.PlayerGui.headsupdisplay.helth.Visible = true
				script.Parent.RemoteEvent:FireServer()
				playing = false
				cam.CameraSubject = char.Humanoid
				cam.CameraType = Enum.CameraType.Custom
				cam.CFrame = char.Head.CFrame
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Visible = false
				plr.PlayerGui.headsupdisplay.weapons.charge.Visible = false
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Position = UDim2.new(0.02, 0, 0.182, 0)
				plr.PlayerGui.headsupdisplay.weapons.charge.bar.Size = UDim2.new(0,0,0,7)
				ts:Create(plr.PlayerGui.headsupdisplay.helth.move, TweenInfo.new(30, Enum.EasingStyle.Linear), {--[[Position = UDim2.new(0.05, 0, 0.04, 0),]] Size = UDim2.new(0.913, 0, 0, 0)}):Play()
				print("a")
				wait(30)
				print("aa")
				ts:Create(plr.PlayerGui.headsupdisplay.helth, TweenInfo.new(2), {BackgroundTransparency = 1}):Play()
				ts:Create(plr.PlayerGui.headsupdisplay.helth.move, TweenInfo.new(2), {BackgroundTransparency = 1}):Play()
				ts:Create(plr.PlayerGui.headsupdisplay.helth.ImageLabel, TweenInfo.new(2), {ImageTransparency = 1}):Play()
				print("aaa")
				wait(2)
				print("aaaa")
				plr.PlayerGui.headsupdisplay.helth.Visible = false

			end
			db = false
		end
	end
end)
1 Like

Wait will yield the current thread until the given “time” has elapsed. No matter task.wait(), nor wait() will change the functionality of that.

It’s definitely a backend bug in your code, not wait(). I recon looking over your code, and trying to debug parts that may not work. (Possibly everything after the “a” print.)

1 Like

task.wait fixed it, thanks a lot

1 Like