I am making a cooking game and I want to make it where if you click it in time, then it gives you a bacon tool or something, but if you don't, in five seconds, it will burn. I already made the burning part but I don't know how to make it where, like I said, if you don't get it in five seconds then it happens. Thanks!
Here is the script:
function onClicked() local ToolNames = {"CookedBacon", "Item", "Item"}-----put name of the tool has to be exact exact name (remove the words items but not (") keep (")Between (" ") put item name for EX: "Glock 17" local Storage = game:GetService("ServerStorage")----Put the tool item in serverstorage (cars,hats,and clothing will not work only tools work like guns and such) local Part = script.Parent local ClickDetector = Part:WaitForChild("ClickDetector") ClickDetector.MouseClick:connect(function(Player) if Player and Player.Character then local Backpack = Player:WaitForChild("Backpack") for i = 1, #ToolNames do local Tool = Storage:FindFirstChild(ToolNames[i]) if Tool then Tool:clone().Parent = Backpack script.Parent.Transparency = 1 script.Parent.SurfaceGui.Enabled = false script.Parent.Parent.Transparency = 1 script.Parent.Parent.Baconn.Transparency = 1 else wait(3) script.Parent.SurfaceGui.TextLabel.Text = "BURNT" script.Parent.Parent.BurntBacon.Transparency = 0 script.Parent.Parent.BurntBacon.Smoke.Enabled = true script.Parent.Parent.Baconn.Transparency = 1 end end end end) end script.Parent.ClickDetector.MouseClick:connect(onClicked)
You can use coroutines to make a wait and not pause the thread (or just put the wait at the end)
Also, using MouseClick will always do a new function and putting a wait wont work.
Use this code:
local connection function onClicked() local ToolNames = {"CookedBacon", "Item", "Item"}-----put name of the tool has to be exact exact name (remove the words items but not (") keep (")Between (" ") put item name for EX: "Glock 17" local Storage = game:GetService("ServerStorage")----Put the tool item in serverstorage (cars,hats,and clothing will not work only tools work like guns and such) local Part = script.Parent local ClickDetector = Part:WaitForChild("ClickDetector") local alreadyFinished = false --// a way to know if the player has already clicked coroutine.resume(corutine.create(function() wait(3) if alreadyFinished == false then script.Parent.SurfaceGui.TextLabel.Text = "BURNT" script.Parent.Parent.BurntBacon.Transparency = 0 script.Parent.Parent.BurntBacon.Smoke.Enabled = true script.Parent.Parent.Baconn.Transparency = 1 end end)) ClickDetector.MouseClick:connect(function(Player) if Player and Player.Character then local Backpack = Player:WaitForChild("Backpack") for i = 1, #ToolNames do local Tool = Storage:FindFirstChild(ToolNames[i]) if Tool then Tool:clone().Parent = Backpack script.Parent.Transparency = 1 script.Parent.SurfaceGui.Enabled = false script.Parent.Parent.Transparency = 1 script.Parent.Parent.Baconn.Transparency = 1 alreadyFinished = true end end end end) end connection = script.Parent.ClickDetector.MouseClick:Connect(onClicked) -- connect is deprecated use Connect instead