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!
Hopefully this explained enough feel free to comment a question if you need more help. Anyways I hope you can skim through this and figure out what I did. Also if you found this helpful please vote up a rep :)
Cre = game:GetService('DataStoreService'):GetDataStore('Credits') Ex = game:GetService('DataStoreService'):GetDataStore('Experience') function load1(plr) --loads players credits return Cre:GetAsync(plr.userId) or Cre:SetAsync(plr.userId, 0) and 0 end function load2(plr) --loads exp function will either set or gets them from last time return Ex:GetAsync(plr.userId) or Ex:SetAsync(plr.userId, 0) and 0 end game.Players.PlayerAdded:connect(function(plr) --player joins local points = Instance.new('IntValue', plr) points.Name = 'Credits' points.Value = load1(plr) --changes the value from the load function local exp = Instance.new('IntValue', plr) exp.Name = 'Experience' exp.Value = load2(plr) --changes the value from the load function end) game.Players.PlayerRemoving:connect(function(plr) --saves it when player leaves Cre:UpdateAsync(plr.userId, function() return plr:FindFirstChild("Credits").Value end) Ex:UpdateAsync(plr.userId, function() return plr:FindFirstChild("Experience").Value end) end)