Removing an item from a part but it still prints

so i am making a cooking game but the problem is even if i remove the sausage it still prints Done

script.Parent.Touched:connect(function(hit)
	if hit.Name == "Sausage" then
		local part = workspace.Sausage
		print("Cooking")
                wait(5)
		print("Done")
	else
		print("Not sausage")
	end
end)

Sorry I understood your question wrong you have to check if the part is still valid after 5 seconds like so:

wait(5)
if part then
   print("Done")
else
   print("It got destroyed before then")
end

The full script would look something like this:

script.Parent.Touched:connect(function(hit)
	if hit.Name == "Sausage" then
		local part = workspace.Sausage
		print("Cooking")
                wait(5)
		if part then
		   print("Done")
		else
		   print("It got destroyed before then")
		end
	else
		print("Not sausage")
	end
end)

Or you can use a while loop to exit when the part gets destroyed…

script.Parent.Touched:connect(function(hit)
	if hit.Name == "Sausage" then
		local part = workspace.Sausage
		print("Cooking")

        local ct = 0
        while ct < 5 do
            if not part then
                break
            end
            ct += 1
            task.wait(1)
        end
	else
		print("Not sausage")
	end

    if not part then return end
    print("Done")
end)

This would be inaccurate. The code should be:

script.Parent.Touched:connect(function(hit)
	if hit.Name == "Sausage" then
		local part = workspace.Sausage
		print("Cooking")

        local ct = 0
        while ct < 5 do
            if not part then
                break
            end
            ct += task.wait(1)
        end
	else
		print("Not sausage")
	end

    if not part then return end
    print("Done")
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.