Help With Data Saving Script

So for clarification:
Remove the Material.Changed and add:
‘’’
game.Players.PlayerRemoving.Connect(function(player)
ds1:SetAsync(player.UserId, Wood.Value)
end)

game.Players.PlayerRemoving.Connect(function(player)
ds2:SetAsync(player.UserId, Stone.Value)
end)

game.Players.PlayerRemoving.Connect(function(player)
ds3:SetAsync(player.UserId, Iron.Value)
end)

game.Players.PlayerRemoving.Connect(function(player)
ds5:SetAsync(player.UserId, Explorite.Value)
end)

‘’’

you can put it all inside of one playerremoving function. Also, you need to do player.leaderstats.Iron.Value not just Iron.Value, as this function goes outside of any other functions or this wont work.

1 Like

So this would work? And I would place this where my current Material.Changed functions are?
‘’’
game.Players.PlayerRemoving.Connect(function(player)

ds1:SetAsync(player.UserId, player.leaderstats.Wood.Value)
ds2:SetAsync(player.UserId, player.leaderstats.Stone.Value)
ds3:SetAsync(player.UserId, player.leaderstats.Iron.Value)
ds5:SetAsync(player.UserId, player.leaderstats.Explorite.Value)

end)

‘’’

yes. That script should work perfectly.

1 Like

I’m testing this… it’s not saving my new data. I’m making a couple changes and seeing if that works.

thats because studio closes the game. For it to work better inside of studio use game:BindToClose. Here is a script I use for my game (ignore the table save unless you want to use it):

local dataservice = game:GetService("DataStoreService")
local statsstore = dataservice:GetDataStore("StatsData")

game.Players.PlayerAdded:Connect(function(player)
	local statstable = statsstore:GetAsync(player.UserId) or {}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = statstable.Gold or 0
	gold.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Value = statstable.XP or 0
	xp.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = statstable.Level
	level.Parent = leaderstats
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stats = {
		["Gold"] = player.leaderstats.Gold.Value,
		["XP"] = player.leaderstats.XP.Value,
		["Level"] = player.leaderstats.Level.Value
	}
	statsstore:SetAsync(player.UserId, stats)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		local stats = {
			["Gold"] = v.leaderstats.Gold.Value,
			["XP"] = v.leaderstats.XP.Value,
			["Level"] = v.leaderstats.Level.Value
		}
		statsstore:SetAsync(v.UserId, stats)
	end
end)
1 Like

Would it work if I re-published the game and tried it on Roblox??? I don’t really need it to work in Studio anyway.

yes, with playerremoving it works inside of in-game roblox. but you can just add the BindToClose function instead of going through the trouble of publishing the game and joining it
Edit: Sorry if I am seeming very repetitive.

1 Like

I’m testing it on Roblox right now… let me see if it works!

It worked! Thank you so much for your help with Explore and Craft (my game)!!! Now it can have a full server and hopefully not drop any requests!

In case you wanted to try it:
https://www.roblox.com/games/5644451718/Explore-And-Craft?refPageId=5211ad94-d87b-4752-b681-dba595c9a699