How to make a function run once and never again?

Okay, I am working on a game which includes an NPC that if you collect for example 10 coins, you get 200 diamonds. How do I make it so you can only complete the mission and if you come back again in another server and also the same server, it runs a different function, like: 'print(“Already completed”) and never lets you complete the mission again on the same account.

Any help is appreciated, thanks in advance.

Use Datastoreservice.
Store the data to the player if the quest is completed.

Yes, but I want to make it so they cannot comeplete the mission again.

DatastoreService is a good thing to use this because if not they can just leave and rejoin, and start the task over again.

Less laggy when a user is leaving
Use GlobalDataStore | Roblox Creator Documentation and make the key to the gameId so it will only need to load all of the people in 1 datastore

Mostly laggy sometimes not when user is leaving
DatastoreService is accurate because it you don’t have to script too much i guess??

I never used :GetGlobalDatastore() so i never know how to use it, sorry.

local used = false

local function giveCoins()
	if used then --if used is true (if used) and the function is called then abruptly end the function
	local used = true
	print("Hello world!")
	--perform code here

giveCoins() --Hello world! is printed
giveCoins() --nothing happens

If this doesn’t need to persist across multiple server instances then you can simply use the logic as exemplified in the above in order to define a function of which its body (code content) can only be executed once.


Well, I’m using it for a one-time quest.