Help with delay without delaying script functions

Hello, I currently am making a Inventory System and there’s so many bugs to fix and I need help with this one, I have a delay value and its a Boolean, every time someone equips a item the value goes to true and it waits 1 second and goes to false, the equip script only works if the value is false, how do i make the delay happen after clicking this without delaying all these functions? so i want a delay after a click but it delays the functions of the click as well.

local selected = script.Parent.Parent.Parent.Handler.Selected
local location = script.Parent.Parent.Parent.Handler.Location
local player = game.Players.LocalPlayer
local character = player.Character
local LastEquipped = script.Parent.Parent.Parent.Handler.LastEquippedLight

script.Parent.MouseButton1Click:connect(function()
	---- Equip Script ----
	if equipped.Value ~= selected.Value then -- Just the same as the last one
		print("WhenDoesThisHappen")
		character.Humanoid:EquipTool(selected.Value)
		script.Parent.Parent.Parent.Parent.Backpack.EquippedS.Disabled = true
		script.Parent.Parent.Parent.Parent.Backpack.Unequipped.Disabled = false
		script.Parent.Parent.Parent.Parent.Backpack.Equipped.Value = false
		if location.Value == player.Backpack then
			print("AintNoWay")
			character.Humanoid:EquipTool(selected.Value)
			equipped.Value = selected.Value
			script.Parent.Text = "Unequip"
			script.Parent.Parent.Parent.Handler.Location.Value = player
			if equipped.Value:GetAttribute("Lantern") then
				equipped.Value = selected.Value
				script.Parent.Text = "Unequip"
				character.Humanoid:EquipTool(selected.Value)
				script.Parent.Parent.Parent.Parent.Backpack.Equipped.Value = true
				script.Parent.Parent.Parent.Parent.Backpack.EquippedS.Disabled = false
					script.Parent.Parent.Parent.Parent.Backpack.Unequipped.Disabled = true
			end
		end
	else
		---- UnEquip Script ----
		if script.Parent.Parent.Parent.Handler.Equipped.Value == script.Parent.Parent.Parent.Handler.LastEquippedLight.Value then
			character.Humanoid:UnequipTools()
			equipped.Value = nil
			script.Parent.Text = "Equip"
			script.Parent.Parent.Parent.Parent.Backpack.Equipped.Value = false
			script.Parent.Parent.Parent.Parent.Backpack.Unequipped.Disabled = false
			script.Parent.Parent.Parent.Parent.Backpack.EquippedS.Disabled = true
			script.Parent.Parent.Parent.Handler.Location.Value = player:WaitForChild("Backpack")
			else
		script.Parent.Parent.Parent.Handler.Location.Value = player:WaitForChild("Backpack")
		character.Humanoid:UnequipTools()
		equipped.Value = nil
			script.Parent.Text = "Equip"
		end
	end
end)

I don’t see where you’re delaying the code, but from how you described your issue, I suggest you utilize task.spawn(), which creates a separate thread for the engine to run without yielding your main function!

The following code demonstrates this concept.

function foo()
	print("Starting foo")
	task.spawn(function() -- Runs as a separate thread 
		wait(2)
		print("Spawn foo")
	end)
	print("Ending foo") -- Reaches this without being delayed by the wait
end

foo()

Here’s a video of the code in action.


As you can see, the function foo runs through without a delay but creates a separate thread to run on top of it.

I strongly recommend using the task.spawn() function as rarely as possible, since it can sometimes lead to inefficient code.

Hope this helps. Let me know if you have any issues.

2 Likes