Hi! I have a data store script which shows the following values:
"Coins"
"Wins"
"Visits"
"R$ Donated"
"Level"
"XP"
"RequiredXP"
What is the problem?
Script:
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local badge = game:GetService("BadgeService")
local function saveData(player)
local tableToSave = {
player.leaderstats.Coins.Value;
player.leaderstats.Wins.Value;
player.Visits.Value;
player["R$ Donated"].Value;
player.leaderstats.Level.Value;
player.XP.Value;
player.RequiredXP.Value;
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data has not been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
Wins.Value = 0
local Visits = Instance.new("IntValue")
Visits.Name = "Visits"
Visits.Parent = player
Visits.Value = 1
local Donated = Instance.new("IntValue")
Donated.Name = "R$ Donated"
Donated.Parent = player
Donated.Value = 0
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
level.Value = 1
local XP = Instance.new("NumberValue")
XP.Name = "XP"
XP.Parent = player
XP.Value = 0
local requiredXP = Instance.new("NumberValue")
requiredXP.Name = "RequiredXP"
requiredXP.Parent = player
requiredXP.Value = level.Value*25
XP.Changed:Connect(function()
if XP.Value >= requiredXP.Value then
requiredXP.Value = level.Value*25
level.Value += 1
XP.Value = 0
end
end)
Wins.Changed:Connect(function()
if player.leaderstats.Wins.Value == 1 then
local success, errmsg = pcall(function()
badge:AwardBadge(player.UserId, 2127079658)
end)
if success then
print("Awarded!")
else
warn("Failed to award badge!")
warn(errmsg)
end
end
if player.leaderstats.Wins.Value == 2 then
local success, errmsg = pcall(function()
badge:AwardBadge(player.UserId, 2127079679)
end)
if success then
print("Awarded!")
else
warn("Failed to award badge!")
warn(errmsg)
end
end
end)
Visits.Changed:Connect(function()
if Visits.Value == 10 then
local success, errmsg = pcall(function()
badge:AwardBadge(player.UserId, 2127209356)
end)
if success then
print("Awarded!")
else
warn("Failed to award badge!")
warn(errmsg)
end
end
if Visits.Value == 25 then
local success, errmsg = pcall(function()
badge:AwardBadge(player.UserId, 2127209520)
end)
if success then
print("Awarded!")
else
warn("Failed to award badge!")
warn(errmsg)
end
end
if Visits.Value == 50 then
local success, errmsg = pcall(function()
badge:AwardBadge(player.UserId, 2127209540)
end)
if success then
print("Awarded!")
else
warn("Failed to award badge!")
warn(errmsg)
end
end
end)
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data[1]
Wins.Value = data[2]
Visits.Value = data[3]
Visits.Value += 1
Donated.Value = data[4]
level.Value = data[5]
XP.Value = data[6]
requiredXP.Value = data[7]
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved!")
else
print("Data has not been saved!")
warn(err)
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved!")
else
print("Data has not been saved!")
warn(err)
end
end
end)
There is an issue with my Level
, XP
and RequiredXP
value. It’s supposed to be set to 1 but the Level
and RequiredXP
are both set to zero.
Please help me fix this. Thanks.
Edit: Forgot to mention that there are no errors or warnings when I run the script.