Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Help With DataStore?

Asked by
novipak 70
10 years ago

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!

0
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. GetSporked 5 — 10y
0
Thanks! And yeah, I'm a beginner at this so I just tried it out. novipak 70 — 10y
0
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. GetSporked 5 — 10y
0
Would doing this with four values be basically copy/paste and then tweaking this a little bit? novipak 70 — 10y
View all comments (6 more)
0
Mhmm yes, and if it worked you might want to do the correct answer thing so people know that it was answered. GetSporked 5 — 10y
0
I tried what you did, except I edited it a bit. I'll add it to my original post. novipak 70 — 10y
0
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. GetSporked 5 — 10y
0
Thank you, but it still isn't working. I'll edit my original post. novipak 70 — 10y
0
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. novipak 70 — 10y
1
Your name is what scares me the most. OniiCh_n 410 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

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)
Ad

Answer this question