I made a leaderstats script but everytime the data store reset like 0 and dont save, i try everything but nothing works, i try change the names and values but nothing
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")
local DataLoad = game.ReplicatedStorage.Remotes:WaitForChild("DataLoad")
local StarterData = {
Gold = 0,
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
plr.hidestats.EXPNeeded.Value = expDefault * leaderstats.Level.Value
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
for index, value in StarterData do
if PlayerData[index] == nil then
PlayerData[index] = value
end
end
end
end
pcall(function()
DataStore:SetAsync(plr.UserId, PlayerData)
end)
end)