Coroutine not working

Hello,
Yes, yes, yes.
I don’t even know how you manage to not save a script, or ever heard of the concept of saving scripts.
API services are obviously turned on in studio or else my leaderstats would just be 0.
Can’t you just give me a solution right on out? I don’t need pcalls if I have a solution right here.
Also, the code you wrote above for me is supposed to be a viable solution, and yes, I do have understanding of what the code does.
The problem is that the coroutine is not running at all, or it would print ‘jiesh’ in the earlier versions of my code.

If you’re imagining the devforum scripting category to write out your scripts with 100% perfection and no errors, then stop with that thought. This category is here to help you with the solution, what you’re thinking of solution is just copy pasting code in my opinion.

Anyways to the point, the reason your coroutine isn’t executing is because when you do coroutine.wrap around a function, it just makes a new thread for that function, but you will need to execute it as a function. Which I have told about 3 times yet, but your script didn’t seem to have the same changes.

Also just tested out the script that I had modified and given (the latest one), and it seems to work fine, it loads the data and everything is good. The most obvious mistake here that you can make is that, you’re updating the data from client side.

After you’ve made sure everything is like that, if you could give your latest code, then we can help you debug that code, because it doesn’t seem like you’re following up.

Hello,
There is absolutely nothing supporting the opinion in which my data is being saved on the client side. I have several videos showing that this is done in and only in a serverscript located in serverscriptservice.
Datastore literally cannot be accessed on the client side and will always give an error when doing so, which there is not.
Back to the topic, I’m sorry that I don’t know anything about coroutines and still don’t know. I don’t even know what a “thread” is or where to put resume or what it does. I’m just using my prior knowledge and quick reads on the developer hub to understand your replies to the best of my ability.
In addition, I don’t know what pcall does, where and how to use it, and what it’s used for.

Hello,
This is my code so far. I used pcall for the function.

local datastore = game:GetService("DataStoreService")
local moneystore = datastore:GetDataStore("Stats")

game.Players.PlayerAdded:Connect(function(p)
	local leader = Instance.new("IntValue",p)
	leader.Name = "leaderstats"
	local mon = Instance.new("NumberValue",leader)
	mon.Name = "Money"
	local so = Instance.new("StringValue",leader)
	so.Name = "Job"
	local ren = Instance.new("NumberValue",leader)
	ren.Name = "Reputation"
	local gor = moneystore:GetAsync(p.UserId)
	if gor~=nil then
		mon.Value = gor[1]
		so.Value = gor[2]
		ren.Value = gor[3]
	else
		gor = {mon.Value,so.Value,ren.Value}
	end
	for i,v in pairs(script.Notice:GetChildren()) do
		local sog = v:Clone()
		sog.Parent = p
	end
end)

local function save(v)
	pcall(coroutine.wrap(function()
		local success, response
		local tries = 0
		local saveData = {v.leaderstats.Money.Value,v.leaderstats.Job.Value,v.leaderstats.Reputation.Value}

		success, response = pcall(moneystore.SetAsync, moneystore, v.UserId, saveData)

		repeat
			wait(7)
			success, response = pcall(moneystore.SetAsync, moneystore, v.UserId, saveData)
			tries += 1
		until not success or tries < 3 
	end)())
end


game.Players.PlayerRemoving:Connect(function(p)
	save(p)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		save(v)
	end
end)


It gives this error:
00:03:44.900 ServerScriptService.Script:28: missing argument #1 - Server - Script:28

What I meant with data being updated through the client is, are you making changes to the leaderstat values from the client side? If so, then switch to remote events and update the data through it. Otherwise it won’t update obviously.

I am pretty sure you didn’t know what pcalls are, so I had given you a really nice tutorial explaining it all above like 4 posts back. I gave you the link to coroutines too, so you can learn from it.

And I am aware with Datastore can’t be accessed from the client side.

If you literally just want the code that seemed to work for me, here it is:

Code
local datastore = game:GetService("DataStoreService")
local moneystore = datastore:GetDataStore("Stats")

game.Players.PlayerAdded:Connect(function(p)
	local leader = Instance.new("IntValue",p)
	leader.Name = "leaderstats"
	local mon = Instance.new("NumberValue",leader)
	mon.Name = "Money"
	local so = Instance.new("StringValue",leader)
	so.Name = "Job"
	local ren = Instance.new("NumberValue",leader)
	ren.Name = "Reputation"
	local gor = moneystore:GetAsync(p.UserId)
	if gor~=nil then
		mon.Value = gor[1]
		so.Value = gor[2]
		ren.Value = gor[3]
	else
		gor = {mon.Value,so.Value,ren.Value}
	end
	for i,v in pairs(script.Notice:GetChildren()) do
		local sog = v:Clone()
		sog.Parent = p
	end
end)

local function save(v)
    coroutine.wrap(function()
		local success, response
		local tries = 0
		local saveData = {v.leaderstats.Money.Value,v.leaderstats.Job.Value,v.leaderstats.Reputation.Value}

		success, response = pcall(moneystore.SetAsync, moneystore, v.UserId, saveData)

		repeat
			wait(7)
			success, response = pcall(moneystore.SetAsync, moneystore, v.UserId, saveData)
			tries += 1
		until not success or tries < 3 
	end)()
end


game.Players.PlayerRemoving:Connect(function(p)
	save(p)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		save(v)
	end
end)

https://developer.roblox.com/en-us/articles/Beginners-Guide-to-Coroutines

Hello,
I am now on phone but before that, I tested out your code, I tried to catch instances of the data store not working on camera, but ultimately failed to do so. However, I did experience it twice off camera. Because it has been a long day, I will just mark your reply as the solution for now, as the game I am working on is not too important. However I have experienced faults in your method ( data not saving sometimes ) Perhaps you think that it works because you have only experienced the times where it saved through player removed, but sometimes it does not run when a game closes. Try running it several times both in game and in studio, changing the values of the leaderstats every time.

I can ensure that the code is working, I tested it again and commented out the player removed block. After the save function is run, its basically the same, and its even loading the data.

--game.Players.PlayerRemoving:Connect(function(p)
--	save(p)
--end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		print("Saving through bind to close.")
		save(v)
	end
end)

Data loss is a tragic thing for anyone and everyone. I am going to have to get my data saving crystal perfect after alpha.

Data loss can be managed properly if you use the correct methods (Unless Roblox datastore crashes or something), I would just recommend you going through pcalls, which help a lot in it.

Could you try printing through the coroutine?

Sometimes my Coroutines just stop working. It’s like if the yield command is just returning from the method and it stop his execution there.

Hello,
Could you attempt printing through the coroutine?

Hello,
Could you possibly attempt using the print() function in a coroutine to prove that the coroutine is working? It does not print anything for me and I have to assume the data saving when a server closes is the work of a PlayerRemoved event.
@MrNicNac If you could help here, that would be wonderful.

Hello,

If possible, would you be able to use the print() function, preferably just printing a random word in output so we can see if the coroutine really works?