i want the stats to be saved and also to make the process of creating the stats simple (in this case via a function) But when you enter the game, a new data is created, which deletes the old one (or something like that) and I don’t know how to fix it. I tried to fix it through tables, but I don’t understand them very well
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
game:GetService("ReplicatedStorage").Remotes.StatsAdd.OnInvoke = function(stat, val)
local player = stat.Parent.Parent
local pointsStore = DataStore2(stat.Name, player)
local text = player.PlayerGui.Statistic.List:FindFirstChild(stat.Name)
pointsStore:Increment(val)
stat.Value = pointsStore:Get(val)
text.Text = text.Name .. ": " .. stat.Value
end
local datas = {}
local function newStat(statistic, name, player)
table.insert(datas, name)
if not (DataStore2:GetAsync(player)) then
DataStore2.Combine("DATA", name)
local pointsStore = DataStore2(name, player)
local stat = Instance.new("NumberValue")
stat.Name = name
stat.Value = pointsStore:Get(0)
stat.Parent = statistic
end
end
game:GetService("Players").PlayerAdded:Connect(function(player)
local statistic = Instance.new("Folder")
statistic.Name = "Stats"
--stats
newStat(statistic, "Deaths", player)
newStat(statistic, "Kills", player)
newStat(statistic, "Food Eaten", player)
newStat(statistic, "People Signed Your Petition", player)
newStat(statistic, "Money Earned", player)
newStat(statistic, "Kicked", player)
newStat(statistic, "Doors Kicked", player)
newStat(statistic, "Glass Destroyed", player)
statistic.Parent = player
--for _,v in pairs(statistic:GetChildren()) do
for _,v in pairs(datas) do
local text = game:GetService("ServerStorage").Text:Clone()
text.Name = v.Name
text.Text = v.Name .. ": " .. v.Value
text.Parent = player.PlayerGui:WaitForChild("Statistic").List
end
end)
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local statistic = player:WaitForChild("Stats")
character.Humanoid.Died:Connect(function()
game:GetService("ReplicatedStorage").Remotes.StatsAdd:Invoke(statistic.Deaths, 1)
end)
end)
end)
OLD CODE WITH NO DATASTORE
game:GetService("ReplicatedStorage").Remotes.StatsAdd.OnInvoke = function(stat, val)
local player = stat.Parent.Parent
local text = player.PlayerGui.Statistic.List:FindFirstChild(stat.Name)
stat.Value += val
text.Text = text.Name .. ": " .. stat.Value
end
local function newStat(statistic, name)
local numbervalue = Instance.new("NumberValue")
numbervalue.Name = name
numbervalue.Parent = statistic
end
game:GetService("Players").PlayerAdded:Connect(function(player)
local statistic = Instance.new("Folder")
statistic.Name = "Stats"
newStat(statistic, "Deaths")
newStat(statistic, "Kills")
newStat(statistic, "Food Eaten")
newStat(statistic, "People Signed Your Petition")
newStat(statistic, "Money Earned")
newStat(statistic, "Kicked")
newStat(statistic, "Doors Kicked")
newStat(statistic, "Glass Destroyed")
statistic.Parent = player
for _,v in pairs(statistic:GetChildren()) do
local text = game:GetService("ServerStorage").Text:Clone()
text.Name = v.Name
text.Text = v.Name .. ": " .. v.Value
text.Parent = player.PlayerGui:WaitForChild("Statistic").List
end
end)
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local statistic = player.Stats
character.Humanoid.Died:Connect(function()
game:GetService("ReplicatedStorage").Remotes.StatsAdd:Invoke(statistic.Deaths, 1)
end)
end)
end)