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

Why won't this datastore work?

Asked by 9 years ago

Okay. In my game, i use datastores to fill out the values on the leaderboard. I am using an edited version of the Roblox U "Mad Bloxxer" scripts. (No, its not another murder game. I think we've all had enough of those) First off, here's where I first set up my datastores.

01function playeradded(player)
02 
03    local mydata = datastore:GetAsync(player.userId) or {}
04    mydata.wins = mydata.wins or 0
05    mydata.BC = mydata.BC or 0
06    mydata.hyperlaser = mydata.hyperlaser or 0
07 
08    playerdata[player.userId] = mydata
09 
10 
11    local leaderstats = Instance.new("Model")
12    leaderstats.Name = "leaderstats"
13    leaderstats.Parent = player
14 
15 
View all 43 lines...

Here is where I award a win to the player if that player won the game (they won the game if they still have 'MatchTag')

01function awardwin(players)
02    if type(players) == type({}) then
03 
04        for i = 1, #players do
05    if players[i]:FindFirstChild("MatchTag") then
06            if players[i]:FindFirstChild("leaderstats") then
07                if  players[i].leaderstats:FindFirstChild("Wins") then
08                    local stat = players[i].leaderstats.Wins
09                    stat.Value = stat.Value + 1
10                    local newstat = players[i].leaderstats:FindFirstChild("Wins")
11                        local key = "User_" .. players[i].userId
12                    datastore:SetAsync(key, function(oldvalue)
13                        local newvalue = oldvalue or 0
14                        newvalue = newvalue + 1
15                    end)
View all 21 lines...

Heres my variables...

01local roundtimer = 90
02local intermissiontimer = 5
03 
04local datastore = game:GetService("DataStoreService"):GetDataStore("playerdata")
05local serverstorage = game:GetService("ServerStorage")
06local replicatedstorage = game:GetService("ReplicatedStorage")
07local debris = game:GetService("Debris")
08local maps = serverstorage:WaitForChild("Maps")
09local mapholder = game.Workspace:WaitForChild("MapHolder")
10local gear = serverstorage:WaitForChild("Gear")
11local statustag = replicatedstorage:WaitForChild("Status")
12local timertag = replicatedstorage:WaitForChild("TimerTag")
13local Winner = replicatedstorage:WaitForChild("Winner")
14local resulttag = replicatedstorage:WaitForChild("result")
15local event = replicatedstorage:WaitForChild("RemoteEvent")
16local firstrun = true
17 
18 
19 
20local playerdata = {}

Finally, here's where its supposed to save the data when the player leaves

01    game.Players.PlayerRemoving:connect(function(player)
02player:WaitForDataReady()
03wait(1)
04local stats = player:FindFirstChild("leaderstats"):GetChildren()
05 
06for i = 1, #stats do
07print(stats[i].Name)
08datastore:SetAsync(stats[i].Name, stats[i].Value)
09end
10end)

Thanks for the help! Please just give me an edited script, not some vague answer with an example. Not that I want to be picky, but I am a very novice scripter, and it's very hard to learn from something so vague. If I wanted vague answers, I'd be using the Wiki ;)

1 answer

Log in to vote
2
Answered by 9 years ago

Datastores hold information. They don't have 'children' therefore you cannot do "mydata.Whatever"

You have to use tables.

01local myData = game:GetService("DataStoreService"):GetDataStore("GameStats")
02local vals = {
03{"Wins",0,"IntValue"--[[Wins in the name of the value, 0 is the value,"IntValue" is the type of value]},
04{"AddAnotherHere?",0,"IntValue"},
05}
06 
07game.Players.PlayerAdded:connect(function(player)
08    local tab = myData:GetAsync(player.userId)
09 
10    if not tab then -- new player set up table
11        tab = {}
12        local num = 0
13        for i = 1,#vals do
14        table.insert(tab,{vals[i][1],vals[i],2})
15        num = num + 1
View all 54 lines...

I'm not 100% sure that'll work, but that's the basic idea. I made it fully customizable for you so you won't have to do as much work :P.

If you have any questions about anything, message me. My username is ObscureEntity!

0
I made it on the spot so... ObscureEntity 294 — 9y
0
Thank you so much! One quick question, on line 21, how does it know what ds is? I dont see it referenced anywhere else. Is that like myData? Antharaziia 75 — 9y
0
Also, to increase a value on the leaderboard, can I just increase the value on the leaderboard since it Antharaziia 75 — 9y
0
... will save it at the end anyway? Antharaziia 75 — 9y
0
Check the message I sent you. ObscureEntity 294 — 9y
Ad

Answer this question