Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't it save the data in this dataStore script?

I'm making a data store script and I can't seem to see an error. There is no error in the output either. Here is my script, pls fix if possible.

local DSS = game:GetService("DataStoreService")
local cashDSS = DSS:GetDataStore("CurrencyStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local Currency = Instance.new("IntValue", leaderstats)
	Currency.Name = "Bucks"
	
	local data
	
	local success, errormsg = pcall(function()
		data = cashDSS:GetAsync(plr.UserId.."-Bucks")
	end)
	
	if success then
		Currency.Value = data
		print("data of "..plr.Name.." has been loaded") --//Prints this
	end
	
	if errormsg then
		print("Data of "..plr.Name.." couldn't be loaded") --//I'm assuming this works
		warn(errormsg)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
		cashDSS:SetAsync(plr.UserId.."-Bucks", plr.leaderstats.Bucks.Value)
	end)
		
	if success then
		print("Successfully saved data of "..plr.Name) --//Doesn't print
	else
		print("Data could not be saved of "..plr.Name) --//Doesn't print
		warn(errormsg) --//Doesn't warn
	end
end)

I appreciate your time and effort. Thanks for helping.

1 answer

✓ Accepted answer

This happened to me when I was making a datastore, but I found a fix. There is a roblox function called game:BindToClose, this event fires when all the players leave and keeps the server open until what is inside the function is done running. I suggest you make a function like SaveData so that way you don't have to put 2 duplicates of your code in 2 places. I really hope this helped.

1 comment

0

Thanks I'll try it out now

This discussion is preserved as it was. New answers, comments, and votes are closed.