So for clarification:
Remove the Material.Changed and add:
‘’’
game.Players.PlayerRemoving.Connect(function(player)
ds1:SetAsync(player.UserId, Wood.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.
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)
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.