How to fire a function and run code underneath instantly?

If you still don’t understand I want to be-able to fire/call a function and run the code underneath it instantly.
Code:

if DatastoreDucks:GetAsync("-ducks") then
			createDucks(Player, DatastoreDucks:GetAsync("-ducks"))
		else
			print(Player.Name.." is a new player. Thanks for joining!")
		end
		
		if DatastoreDucks:GetAsync("-leaderstats") then
			stars.Value = DatastoreDucks:GetAsync("-leaderstats")
			game.Workspace.Baseplate:Destroy()
		else
			game.Workspace.Baseplate.BrickColor = BrickColor.Green()
		end
createDucks(Player, DatastoreDucks:GetAsync("-ducks"))

This function has a loop that will run forever and so it won’t continue on with the script.
All of the code:

local DataStore = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DatastoreDucks = DataStore:GetDataStore("ftDucks")

local function saveData(Player, duckArray)
	pcall(function()
		print("Saving...")
		DatastoreDucks:SetAsync("-ducks", duckArray)
		print("Saved duckstats")
		DatastoreDucks:SetAsync("-leaderstats", Player:FindFirstChild("leaderstats"):FindFirstChild("Stars").Value)
		print("Saved leaderstats")
	end)
end

local function autoSaveText(Player)
	local AutoSaveText = Player.PlayerGui:FindFirstChild("MainDuck"):FindFirstChild("Autosave")
	local DotAmount = {".", "..", "..."}
	
	AutoSaveText.Visible = true
	
	for i = 1, 3 do
		wait(0.85)
		AutoSaveText.Text = "Autosaving"..DotAmount[i]..""
		
		if i == 3 then
			wait(2)
			AutoSaveText.Text = "Saved data."
			AutoSaveText.TextColor3 = Color3.fromRGB(255, 255, 127)
			wait(2)
			AutoSaveText.Visible = false
			AutoSaveText.Text = "Autosaving"
			AutoSaveText.TextColor3 = Color3.fromRGB(0, 0, 0)
		end
	end
	
end

local function autoSave(Player, duckFolder)
	while true do
		task.wait(10)

		if #duckFolder:GetChildren() < 1 then
			print(Player.Name.." does not have any ducks unlocked! :(")
		else
			local duckArray = {}

			for _,duck in pairs(duckFolder:GetChildren()) do
				table.insert(duckArray, duck.Name)
			end

			autoSaveText(Player)
			saveData(Player, duckArray)
		end

	end
end

local function createDucks(Player, DuckArray)
	local duckFolder = Player:FindFirstChild("Ducks")
	
	for i = 1, #DuckArray do
		local newDuck = Instance.new("BoolValue")
		newDuck.Name = DuckArray[i]
		newDuck.Value = true
		newDuck.Parent = Player:FindFirstChild("Ducks")
	end
	
	autoSave(Player, duckFolder)
end

Players.PlayerAdded:Connect(function(Player)
	pcall(function()
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = Player
		
		local stars = Instance.new("IntValue")
		stars.Name = "Stars"
		stars.Value = 0
		stars.Parent = leaderstats
		
		local duckFolder = Instance.new("Folder")
		duckFolder.Name = "Ducks"
		duckFolder.Parent = Player
		
		if DatastoreDucks:GetAsync("-ducks") then
			createDucks(Player, DatastoreDucks:GetAsync("-ducks"))
		else
			print(Player.Name.." is a new player. Thanks for joining!")
		end
		
		if DatastoreDucks:GetAsync("-leaderstats") then
			stars.Value = DatastoreDucks:GetAsync("-leaderstats")
			game.Workspace.Baseplate:Destroy()
		else
			game.Workspace.Baseplate.BrickColor = BrickColor.Green()
		end
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	pcall(function()
		local duckFolder = Player:FindFirstChild("Ducks")
		if #duckFolder:GetChildren() < 1 then return end
		
		local duckArray = {}
		
		for _,duck in pairs(duckFolder:GetChildren()) do
			table.insert(duckArray, duck.Name)
		end
		
		saveData(Player, duckArray)
	end)
end)

game:BindToClose(function()
	wait(3)
end)

Please help me. Thank you!

1 Like

Umm, you can’t really do that. Even if it seems instant nothing is. It’s just running so fast that it appears instant. Also, you appear to be working with DataStores. This makes it even trickier because GlobalDataStore | Roblox Creator Documentation is a yielding function. This means it will pause the thread it’s running in until a result is ready to be returned. I would recommend checking out the Saving Data | Roblox Creator Documentation tutorial.

Also, on a separate note, you should really have this wrapped up in a pcall.

I’ve had no problems with my datastore yet, but thanks.

If I remove the loop the script, it will work again and get the leaderstats.

Technically it cant be run instantly, however it can be run as soon as possible (right after the script yields, so after any async/wait function) with the coroutine.wrap function.

You can use task.spawn for this

task.spawn(function()
    wait(3)
    print("Hey")
end)
task.wait(3)
print ("Hi") 

-- They should in theory print at the same time.

So can I put this in my function and it will continue with the bottom code?
Like this

local function cool()
     task.spawn(function()

     end)
end)

cool()

Well. It’s sort of is similar to coroutine.wrap

Basically you call a function through it and it will create a new thread. It’s used for when you expect code to yield e.g. looping over objects

local function YieldingFunction(argument) -- Argument is v

    -- Somewhere here yields
end

for I, v in ipairs (List) do
    task.spawn(YieldingFunction, v) -- prevents Loop from waiting for the function to finish.
end
1 Like

Instead of a loop you could use RunService.

Thank you! I will test this right now.

I like loops better but thanks for the suggestion!

A new thread should do what you’re asking, but are you planning an endless loop every 10 seconds for each player until the player leaves and it errors?

1 Like

I’ve already fixed that issue, thanks for looking out though! :slight_smile: