Hey, I am trying to make a Level Value in my game, and I am wondering how I can make the Level Value, set to Level 1 if the player is new. How can I make this work correctly?
Here is my main script for DataStore:
-- leaderstats
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MainPlayerData")
local Players = game:GetService("Players")
local function saveData(player)
local Key = "Player_"..player.UserId
local success, err = pcall(function()
local data = {
player.leaderstats.Points.Value,
player.leaderstats.Rubys.Value,
player.leaderstats.playTime.Value,
player.leaderstats.Deaths.Value,
player.leaderstats.Kills.Value,
}
DataStore:SetAsync(Key, data)
end)
if (not success) then
return warn("Player Data Did Not Save")
end
print("Data Saved Successfully")
end
local function loadData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
local Rubys = Instance.new("IntValue")
Rubys.Name = "Rubys"
Rubys.Parent = leaderstats
local playTime = Instance.new("IntValue")
playTime.Name = "playTime"
playTime.Parent = leaderstats
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = leaderstats
local roundKills = Instance.new("IntValue", player)
roundKills.Name = "RoundKills"
local Wins = Instance.new("IntValue", leaderstats)
Wins.Name = "Wins"
leaderstats.Parent = player
player.CharacterAdded:Connect(function(char)
local humanoid = char:WaitForChild("Humanoid", 2)
if humanoid then
humanoid.Died:Connect(function()
Deaths.Value = Deaths.Value + 1
local tag = humanoid:WaitForChild("creator", 2)
if not tag then
--[[ We could construct a new object to put in the humanoid here,
but will error and return here as we can't continue. ]]
error("Failed to find "..tag" object in the Humanoid.")
return
end
--[[ This was returning a string here, we need to get the player object of the killer!
We can also use :FindFirstChild() here as the player should almost definitely exist here.]]
local killer = Players:FindFirstChild(tag.Value)
if tag and killer then
-- Leaderstats may not exist, so we'll find this properly too.
local leaderstats = killer:WaitForChild("leaderstats", 2)
if not leaderstats then
error("Failed to find leaderstats folder in the killer.")
return
end
-- Same with the round kills.
local roundKillsObj = killer:FindFirstChild("RoundKills")
if not roundKillsObj then
error("Failed to find the 'RoundKills' object in the killer.")
return
end
roundKillsObj.Value += 1
--[[ From this point, we should be certain that the values in leaderstats will
exist, but we'll use :FindFirstChild() anyway. ]]
local killsValueObj = leaderstats:FindFirstChild("Kills")
if not killsValueObj then
error("Failed to find the 'Kills' object in the leaderstats folder.")
return
end
killsValueObj.Value += 1
end
end)
end
end)
local success, err = pcall(function()
local key = "Player_"..player.UserId
local data = DataStore:GetAsync(key)
if data then
Points.Value = data[1]
Rubys.Value = data[2]
playTime.Value = data[3]
Deaths.Value = data[4]
Kills.Value = data[5]
end
end)
if (not success) then
player:Kick("Data Failed To Load.")
return warn("Player Data Did Not Save.")
end
print("Data Loaded Successfully.")
while true do
wait(60)
playTime.Value = playTime.Value + 60
end
end
game.Players.PlayerAdded:Connect(function(player)
loadData(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do
saveData(v)
end
end)
Thanks for your time,