Why isn't my delay function not executing?

local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")

local Tool = script.Parent
local enabled = true
local THROW_FORCE = math.random(40, 180)
local MIN_THROW_DELAY = 1

local DECAL_ID = "rbxassetid://13510998982"
local IMPACT_SOUND_ID = "rbxassetid://8595980577"
local NEW_FACE_ASSET_ID = 10416179591 -- No "rbxassetid://"
local FACE_REACT_SOUND_ID = "rbxassetid://154157312"
local ANIMATION_ID = "rbxassetid://75457432354440"

local canThrow = true
local quietKidHit = false -- Debounce

local MouseUpdate = Instance.new("RemoteEvent")
MouseUpdate.Name = "MouseUpdate"
MouseUpdate.Parent = Tool

local currentMousePosition = Vector3.new(0, 0, 0)

MouseUpdate.OnServerEvent:Connect(function(player, mousePos)
	currentMousePosition = mousePos
end)

local function onActivated()
	if not enabled or not canThrow then return end

	enabled = false
	canThrow = false

	local character = Tool.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then return end

	local torso = character:FindFirstChild("Torso") or character:FindFirstChild("HumanoidRootPart")
	if not torso then return end

	local handle = Tool.Handle:Clone()
	handle.Name = "Food_Pizza"
	handle.Anchored = false
	handle.CanCollide = true
	handle.Parent = workspace
	handle.CFrame = Tool.Handle.CFrame

	local throwDirection = (currentMousePosition - handle.Position).Unit
	handle:ApplyImpulse(throwDirection * THROW_FORCE * handle:GetMass())
	handle:SetNetworkOwner(nil)

	Tool.Enabled = false

	if Tool.Handle:FindFirstChild("BiteSound") then
		Tool.Handle.BiteSound:Play()
	end

	local function applyImpactEffects(hit)
		print("applyImpactEffects called. Hit:", hit) -- DEBUG

		if hit and hit.Parent and hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= character then
			local decal = Instance.new("Decal")
			decal.Texture = DECAL_ID
			decal.Name = "decal_"
			decal.Face = Enum.NormalId.Front
			decal.Parent = hit
			game.Debris:AddItem(decal, 5)

			local emitter = Instance.new("ParticleEmitter")
			emitter.Parent = hit
			emitter.Rate = 10
			emitter.Lifetime = NumberRange.new(0.5, 1)
			game.Debris:AddItem(emitter, 1)

			local sound = Instance.new("Sound")
			sound.SoundId = IMPACT_SOUND_ID
			sound.Parent = hit
			sound.Volume = 0.5
			sound:Play()

			local humanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				local sound2 = Instance.new("Sound")
				sound2.SoundId = IMPACT_SOUND_ID
				sound2.Parent = humanoidRootPart
				sound2.Volume = 0.5
				sound2:Play()
			end

			if hit.Parent.Name == "Quiet Kid" and not quietKidHit then --DEBOUNCE
				print("Hit Quiet Kid") -- DEBUG
				quietKidHit = true --DEBOUNCE
				local qHead = hit.Parent:FindFirstChild("Head")
				local qHumanoid = hit.Parent:FindFirstChild("Humanoid")

				if qHead and qHumanoid then
					print("Quiet Kid has Head and Humanoid") -- DEBUG
					local originalFace = qHead:FindFirstChild("face")

					if originalFace then -- Check if originalFace exists
						print("Quiet Kid has originalFace") -- DEBUG
						local originalFaceClone = originalFace:Clone()

						if originalFace then
							originalFace:Destroy()
						end

						qHead.Color = Color3.new(1, 0, 0) -- Red
						local newFace = nil
						local success, newFaceModel = pcall(function()
							return InsertService:LoadAsset(NEW_FACE_ASSET_ID)
						end)

						if success and newFaceModel then
							newFace = newFaceModel:FindFirstChildOfClass("Decal")
							if not newFace then
								for _, child in pairs(newFaceModel:GetDescendants()) do
									if child:IsA("Decal") then
										newFace = child
										break
									end
								end
							end

							if newFace then
								newFace.Parent = qHead
								newFace.Face = Enum.NormalId.Front
							end

							newFaceModel:Destroy()
						end

						local sound4 = Instance.new("Sound")
						sound4.SoundId = FACE_REACT_SOUND_ID
						sound4.Parent = qHead
						sound4.Volume = 0.5
						sound4:Play()

						local animation = Instance.new("Animation")
						animation.AnimationId = ANIMATION_ID
						local animTrack = qHumanoid:LoadAnimation(animation)
						animTrack:Play()
						animTrack:AdjustSpeed(1)




						delay(2, function()
							if qHead then
								-- Restore the original face
								if newFace then
									newFace:Destroy() -- Remove new face
								end

								originalFaceClone.Parent = qHead -- Put original face back

								qHead.Color = Color3.new(1, 1, 0)
							end

						end)
					end -- close if originalFace then
				end -- close if qHead and qHumanoid then
			end -- close if hit.Parent.Name == "Quiet Kid" and not quietKidHit then
		end -- close if hit and hit.Parent and hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= character then
	end -- close local function applyImpactEffects(hit)

	local connection = handle.Touched:Connect(applyImpactEffects)

	game.Debris:AddItem(handle, Players.NumPlayers >= 3 and 3 or 3000)

	task.wait(0.5)

	local player = Players:GetPlayerFromCharacter(character)
	if player and player:FindFirstChild("Backpack") then
		Tool:Destroy()
	end

	task.wait(MIN_THROW_DELAY)
	quietKidHit = false -- Reset for next throw

	if connection then connection:Disconnect() end
	Tool.Enabled = true
	enabled = true
	canThrow = true
end

local function onEquipped()
	if Tool.Handle:FindFirstChild("EquipSound") then
		Tool.Handle.EquipSound:Play()
	end
end

Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)

I’m trying to making it where after 2.5 seconds, the NPC goes back to it’s original state.

We are looking at this part of the code in particular


if hit.Parent.Name == "Quiet Kid" and not quietKidHit then --DEBOUNCE
				print("Hit Quiet Kid") -- DEBUG
				quietKidHit = true --DEBOUNCE
				local qHead = hit.Parent:FindFirstChild("Head")
				local qHumanoid = hit.Parent:FindFirstChild("Humanoid")

				if qHead and qHumanoid then
					print("Quiet Kid has Head and Humanoid") -- DEBUG
					local originalFace = qHead:FindFirstChild("face")

					if originalFace then -- Check if originalFace exists
						print("Quiet Kid has originalFace") -- DEBUG
						local originalFaceClone = originalFace:Clone()

						if originalFace then
							originalFace:Destroy()
						end

						qHead.Color = Color3.new(1, 0, 0) -- Red
						local newFace = nil
						local success, newFaceModel = pcall(function()
							return InsertService:LoadAsset(NEW_FACE_ASSET_ID)
						end)

						if success and newFaceModel then
							newFace = newFaceModel:FindFirstChildOfClass("Decal")
							if not newFace then
								for _, child in pairs(newFaceModel:GetDescendants()) do
									if child:IsA("Decal") then
										newFace = child
										break
									end
								end
							end

							if newFace then
								newFace.Parent = qHead
								newFace.Face = Enum.NormalId.Front
							end

							newFaceModel:Destroy()
						end

						local sound4 = Instance.new("Sound")
						sound4.SoundId = FACE_REACT_SOUND_ID
						sound4.Parent = qHead
						sound4.Volume = 0.5
						sound4:Play()

						local animation = Instance.new("Animation")
						animation.AnimationId = ANIMATION_ID
						local animTrack = qHumanoid:LoadAnimation(animation)
						animTrack:Play()
						animTrack:AdjustSpeed(1)




						delay(2, function()
							if qHead then
								-- Restore the original face
								if newFace then
									newFace:Destroy() -- Remove new face
								end

								originalFaceClone.Parent = qHead -- Put original face back

								qHead.Color = Color3.new(1, 1, 0)
							end

						end)
					end -- close if originalFace then
				end -- close if qHead and qHumanoid then
			end --

use task.delay. Delay is deprecated and should not be used at any costs.

EDIT: Example of usage.

task.delay(5, function(str)
    print(str) -- Will print "hi"
end, "hi")
1 Like

You’re destroying the tool 0.5s after you call onActivated

2 Likes