Need to call a function with a delay

I want to call a function with a delay. What I mean, is that I want to wait 5 seconds before actually calling the function.

game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(grabFromFridge)
game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(useGrillToCook)

I want to call the grabFromFridge function, and then wait for that to finish, then call the useGrillToCook function. Is there a way to do this?

put a task.wait between / at the top

more info on wait (click here)

task.wait yields/halts the thread, what he would want is delay(time, func) (which the title suggests, but not the description). OP should be more clear.

1 Like
ReplicatedStorage.YourEvent.OnServerEvent(function()
  firstFunction()
  task.wait(5)
  secondFunction()
end)

task.wait() is completly fine, no need to create a new thread using task.delay() in this case.

1 Like

This is great, but I’m getting one of the parameters for the useGrillToCook function from FireAvocado.OnServerEvent, so when I try to use that parameter in the function, it just calls a nil value.

Show me your code and I’ll be happy to solve the issue for you.
Both client and server, please.

It is probably nil because the object only exist on the client-side and not the server (in some cases), but as mentioned above, I’ll investigate if you provide the code.

1 Like

is this what you mean?

task.delay(time,function()
-- code here
end)

Here is the client, which is firing the remote event:

local CollectionService = game:GetService("CollectionService")
local Grills = CollectionService:GetTagged("Grill")
local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local cookingGui = playerGui:WaitForChild("CookingGUI")
local cookingGuiFrame = cookingGui:WaitForChild("CookingGUIFrame")

local selectedGrill

for i, grill in pairs(Grills) do
	grill.Grill.CookPrompt.Triggered:Connect(function(plr)
		if plr == game.Players.LocalPlayer then
			selectedGrill = grill
			cookingGuiFrame.Visible = true
		end
	end)
end

cookingGuiFrame.Appetizers.AvocadoToast.MouseButton1Click:Connect(function()
	cookingGuiFrame.Visible = false
	game.ReplicatedStorage.FireAppetizers.FireAvocado:FireServer(selectedGrill)
end)

Here is the server, which is animating the player:

local ovenHeat = script.Parent.OvenGlow

local function useGrillToCook(plr,grill)
	local character = plr.Character
	if not character or not character.Parent then
		character = plr.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local useGrill = Instance.new("Animation")
	useGrill.AnimationId = "rbxassetid://9239035638"
	local useGrillTrack = animator:LoadAnimation(useGrill)
	
	local grillFire = Instance.new("Fire")
	grillFire.Parent = grillCooker
	wait(0.4)
	useGrillTrack:Play()
	useGrillTrack.Stopped:Wait()
	wait(2)
	grillFire:Destroy()
end

local function grabFromFridge(plr,grill)
	local character = plr.Character
	if not character or not character.Parent then
		character = plr.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local grabFromFridge = Instance.new("Animation")
	grabFromFridge.AnimationId = "rbxassetid://9232237218"
	local grabFromFridgeTrack = animator:LoadAnimation(grabFromFridge)
	
	openFridge:Play()
	wait(1.4)
	grabFromFridgeTrack:Play()
	grabFromFridgeTrack.Stopped:Wait()
	wait(0.4)
	closeFridge:Play()
end

game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(grabFromFridge)
game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(delay(5,useGrillToCook))

wait hold up, just wondering you have 2 different functions on the server, but however they are called by the same remote event

and also could you try to combine both functions?

server

I tried my best to combine the functions, so basically the fridge animation plays first then the
stove after 5 seconds

and basically spawn is just like delay because it creates another thread, but without the halt

local ovenHeat = script.Parent.OvenGlow

local function somethingFunc(plr,grill)
	local character = plr.Character
	if not character or not character.Parent then
		character = plr.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	
	local grabFromFridge = Instance.new("Animation")
	grabFromFridge.AnimationId = "rbxassetid://9232237218"
	local grabFromFridgeTrack = animator:LoadAnimation(grabFromFridge)
	
	local useGrill = Instance.new("Animation")
	useGrill.AnimationId = "rbxassetid://9239035638"
	local useGrillTrack = animator:LoadAnimation(useGrill)
	
	task.spawn(function()
		openFridge:Play()
		wait(1.4)
		grabFromFridgeTrack:Play()
		grabFromFridgeTrack.Stopped:Wait()
		wait(0.4)
		closeFridge:Play()
	end)
	
	task.delay(5,function()
		local grillFire = Instance.new("Fire")
		grillFire.Parent = grillCooker
		wait(0.4)
		useGrillTrack:Play()
		useGrillTrack.Stopped:Wait()
		wait(2)
		grillFire:Destroy()
	end)
	
end

game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(somethingFunc)

yeah and it doesn’t seem like both the grill parameters/arguments for each function isn’t actually being used, maybe you’re using it but it doesn’t appear like it in the script you provided

also to be honest i kinda hate using remote events, and they sometimes don’t work how they’re supposed to

Well, I have 3 different functions on the server, but the other one is just the fridge one with a different animation. I could use your solution, but there are many remote events. What I’m trying to achieve is to make a cooking station. There are 30 different dishes, and there will be many different processes, so I would need a lot of functions to do that, but it could work.

uh alright, you might have to use a different remote event for each function

for your sake, you should only one function to receive the same remote event, i think having more than 1 would make it more confusing

game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(grabFromFridge)
game.ReplicatedStorage.FireAppetizers.FireAvocado.OnServerEvent:Connect(delay(5,useGrillToCook))

and also how are the remote events triggered? is it a button on a gui?

and just curious about the different dishes? how would they work?

Yes, the events are triggered by a button on a gui. What do you mean by how do the dishes work?

yeah like are the function like making food such as making a pizza by first adding the dough, then the tomato sauce, then the cheese, and lastly the toppings

and you’re going to have multiple dishes that might have step by step process?

Yes. So there might be spaghetti where I boil the water then cook it.

sorry for late reply but, i think you’re gonna need tons of remote events trying to handle each function and it could get quite messy trying to deal with them

i got one solution, but be careful it is not beginner friendly, nor is it easy to use for the first time and it can overcomplicate things, and to be honest i think remote events work just fine

its called knit and its a game framework and there are pros and cons using it, some say its easy to use while others say its complicated to use, but however it helps out with client and server communications,

i think it should be used for medium to large scale projects, otherwise i think its overkill if its a small project

not sure how good your coding experience, but i’d say kinda complicated to use and if you got questions about it, i’ll do my best to answer them

Thank you! This is a really big project, and I have decent coding knowledge, so I’ll let you know if I need help.