When I was testing if this script works (which is about making leaderstats and saving player’s data) when I leave the game it would give me an error which says:
Attempt to index nil with ‘Points’
I know that it means that there’s nothing with the Points value since “nil” means nothing but how?
here’s the code:
Script
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
Points.Value = 0
local Crystals = Instance.new("IntValue")
Crystals.Name = "Crystals"
Crystals.Parent = leaderstats
Crystals.Value = 0
local playerUserID = "Player_"..player.UserId
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserID)
end)
if success then
Points.Value = data.Points
Crystals.Value = data.Crystals
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserID = "Player_"..player.UserId
local data = {
Points = player.leaderstats.Points.Value;
Crystals = player.leaderstats.Crystals.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserID, data)
end)
if success then
print("success!!!")
else
print("error...")
warn(errormessage)
end
end)
As you can see I even have a pcall function (protection call function) that warns me if something went wrong with yellow text in the output but the error was in red text that means it haven’t even reached to the warn(errormessage) line?
Idk man this is confusing, someone please tell me what is happening it doesn’t even say "Attempt to index nil with ‘Crystals’ and I have Crystals there too not only points…
Apparently because leaderstats doesn’t exist. It might help to have some way to be 100% sure leaderstats will be there. By using FindFirstChild followed by an if statement later on. I honestly have no idea why leaderstats is nil here, but I think this might help.
Ok, I have now tested your code in-game and I’ve seen the error.
The error is here
local Crystals = Instance.new("IntValue")
Crystals.Name = "Crystals"
Crystals.Parent = leaderstats
Crystals.Value = 0
local playerUserID = "Player_"..player.UserId
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserID)
end)
if success then
Points.Value = data.Points
Crystals.Value = data.Crystals
end
end)
The reason this doesn’t work is because there is no saved data.Points
Try this and see if it works:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
Points.Value = 0
local Crystals = Instance.new("IntValue")
Crystals.Name = "Crystals"
Crystals.Parent = leaderstats
Crystals.Value = 0
local playerUserID = "Player_"..player.UserId
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserID)
datapoints = data.Points -- Adding a variable for this so it only succeeds if it's actually there
end)
if success then
Points.Value = data.Points --Error happens here because data.Points doesn't exist
Crystals.Value = data.Crystals
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserID = "Player_"..player.UserId
local data = {
Points = player.leaderstats.Points.Value;
Crystals = player.leaderstats.Crystals.Value;
}
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserID, data)
end)
if success then
print("success!!!")
else
print("error...")
warn(errormessage)
end
end)
Hope this helps, tell me if you’re still having issues!