I’m having a problem that every time I add a new value to the players’ data store, the players that already had data saved before do not receive this new value, so I wanted to know a way for the players whenever I put some new value in the data store to receive this value and not lose the ones they already had
Just if your guys want to know this is the script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")
local DataLoad = game.ReplicatedStorage.Remotes:WaitForChild("DataLoad")
local StarterData = {
Gold = 250,
Level = 1,
Energy = 0,
EXP = 0,
Gems = 0,
EXPNeeded = 125,
EnergyAddons = 1
}
game.Players.PlayerAdded:Connect(function(plr)
local sucess, PlayerData = pcall(function()
return DataStore:GetAsync(plr.UserId) or StarterData
end)
if sucess then
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local hidestats = Instance.new("Folder")
hidestats.Name = "hidestats"
hidestats.Parent = plr
for Index, Value in pairs(PlayerData) do
if Index ~= "EXPNeeded" and Index ~= "EXP" and Index ~= "EnergyAddons" then
local NumberValue = Instance.new("IntValue")
NumberValue.Parent = leaderstats
NumberValue.Name = Index
NumberValue.Value = Value
else
local NumberValue = Instance.new("IntValue")
NumberValue.Parent = hidestats
NumberValue.Name = Index
NumberValue.Value = Value
end
end
task.wait(1)
DataLoad:FireClient(plr)
hidestats.EXP.Changed:Connect(function()
local expDefault = 125
local expNeeded = expDefault * leaderstats.Level.Value
if hidestats.EXP.Value >= expNeeded then
hidestats.EXP.Value = 0
hidestats.EXPNeeded.Value = expNeeded
leaderstats.Level.Value = leaderstats.Level.Value + 1
end
end)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local PlayerData = {
}
for Index, Child in pairs(plr.leaderstats:GetChildren()and plr.hidestats:GetChildren()) do
if Child:IsA("IntValue") then
PlayerData[Child.Name] = Child.Value
end
end
pcall(function()
DataStore:SetAsync(plr.UserId, PlayerData)
end)
end)