So, I knew I wanted Data Persistence in my game, it was a must. But I also knew that DataStores was the new "system" if you will of Data Persistence. So I did a little bit of tinkering with my current leaderboard script which did nothing but make leaderstats and other variables, and tried to add DataStore capability.
Here is my code:
levl = game:GetService('DataStoreService'):GetDataStore('Level')
EXP = game:GetService('DataStoreService'):GetDataStore('Experience')
mon = game:GetService('DataStoreService'):GetDataStore('Money')
pnts = game:GetService('DataStoreService'):GetDataStore('Points')
function load1(plr) --loads players credits
return levl:GetAsync(plr.userId) or levl:SetAsync(plr.userId, 1) and 0
end
function load2(plr) --loads exp function will either set or gets them from last time
return EXP:GetAsync(plr.userId) or EXP:SetAsync(plr.userId, 0) and 0
end
function load3(plr) --loads exp function will either set or gets them from last time
return mon:GetAsync(plr.userId) or mon:SetAsync(plr.userId, 0) and 0
end
function load4(plr) --loads exp function will either set or gets them from last time
return pnts:GetAsync(plr.userId) or pnts:SetAsync(plr.userId, 0) and 0
end
game.Players.PlayerAdded:connect(function(plr) --player joins
local ls = Instance.new("IntValue",plr)
ls.Name = "leaderstats"
local lvl = Instance.new('IntValue', ls)
lvl.Name = 'Level'
lvl.Value = load1(plr) --changes the value from the load function
local xp = Instance.new('IntValue', ls)
xp.Name = 'Experience'
xp.Value = load2(plr)
local money = Instance.new("IntValue", ls)
money.Name = 'Money'
money.Value = load3(plr)
local points = Instance.new("IntValue", ls)
points.Name = 'Points' --changes the value from the load function
points.Value = load4(plr)
end)
game.Players.PlayerRemoving:connect(function(plr) --saves it when player leaves
levl:UpdateAsync(plr.userId, function() return
plr:FindFirstChild("Level").Value end)
EXP:UpdateAsync(plr.userId, function() return
plr:FindFirstChild("Experience").Value end)
mon:UpdateAsync(plr.userId, function() return
plr:FindFirstChild("Money").Value end)
pnts:UpdateAsync(plr.userId, function() return
plr:FindFirstChild("Points").Value end)
end)
Upon touching a brick with the following code:
script.Parent.Touched:connect(function(hit)
humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
player = hit.Players:GetPlayerFromCharacter(hit.Parent)
points = player.leaderstats.Points.Value
points = points + 1
end
end)
I got this error: 04:01:36.475 - Workspace.Leaderboard:46: attempt to index a nil value 04:01:36.475 - Stack Begin 04:01:36.475 - Script 'Workspace.Leaderboard', Line 46 04:01:36.476 - Stack End
Not sure what's wrong with it, any ideas?
I'm a beginner, so it would be very helpful if you could attempt to explain yourself! Thank you!

10 comments
You set the variable datastore more than once so i'm pretty 99.9% sure that would be a problem. I will send you a example script in a answer in a second.
Thanks! And yeah, I'm a beginner at this so I just tried it out.
Ops, Change the exp.Value = load3(plr) to exp.Value = load2(plr). Sorry about that just got a snip of my code from my datastore. I actually had a lot of trouble with this also.
Would doing this with four values be basically copy/paste and then tweaking this a little bit?
Mhmm yes, and if it worked you might want to do the correct answer thing so people know that it was answered.
I tried what you did, except I edited it a bit. I'll add it to my original post.
Down inside the save function instead of pnts.Value do plr:FindFirstChild("Points").Value. So basicly get the int value from there player. Sorry about that something with my scripts reverted so that error effected my game and this answer to.
Thank you, but it still isn't working. I'll edit my original post.
Hold on. The leaderboard is now working, so I made a brick with a touched event in it that should add one point to whoever touch's it point count, I'll post it above.
Your name is what scares me the most.