No clue what’s going on, there’s constantly the error of:
ServerScriptService.SaveService:39: attempt to index nil with 'statsOne'
Anyone got any solution to this? Whole code is here:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
local statsOne = Instance.new("IntValue")
local statsTwo = Instance.new("IntValue")
local Coins = Instance.new("NumberValue", player)
local swordbought = Instance.new("Folder", player)
swordbought.Name = "SwordsBought"
Coins.Name = "Coins"
local statsFour = Instance.new("NumberValue")
statsFour.Name = "statsFour"
statsFour.Parent = leaderstats
local Died = Instance.new("BoolValue", player)
Died.Value = false
Died.Name = "Died"
statsOne.Name = "statsOne"
statsTwo.Name = "statsTwo"
leaderstats.Name = "leaderstats"
statsOne.Parent = leaderstats
statsTwo.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
statsOne.Value = data.statsOne
statsTwo.Value = data.statsTwo
Coins.Value = data.Coins
statsFour.Value = data.statsFour
--EXP_F.Value = data.EXP_F
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = {
statsOne = player.leaderstats.statsOne.Value;
statsTwo = player.leaderstats.statsTwo.Value;
Coins = player.Coins.Value;
statsFour = player.leaderstats.statsFour.Value;
--EXP_F = player.leaderstats.EXP_F.Value
}
local success,errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error while saving!")
warn(errormessage)
end
end)