|
| 02 Jan 2015 05:59 PM |
So I'm trying to recall values from my playerData table and I'm left with this error in the output: "attempt to index global 'playerData' (a nil value)". All of the values save with no problem (save on a constant loop), but when that same player rejoins their saved values don't load. I've provided the script below. Please help me out. I've been at this all day with no success.
--Stored player data script
store = game:GetService("DataStoreService"):GetDataStore("GameDataTest")
game.Players.PlayerAdded:connect(function(player) print("A player has connected. Creating data values...")
plr = player player:WaitForDataReady() pKey = player.userId ps = Instance.new("IntValue", player) ps.Name = "playerStats" LEVEL, XP, PVP, VIP, BSB, HTTYD = Instance.new("IntValue", ps), Instance.new("IntValue", ps), Instance.new("IntValue", ps), Instance.new("BoolValue", ps), Instance.new("BoolValue", ps), Instance.new("BoolValue", ps) LEVEL.Name = "Level" XP.Name = "XP" PVP.Name = "PVP" VIP.Name = "VIP" BSB.Name = "BSB" HTTYD.Name = "HTTYD"
if store:GetAsync(pKey, playerData) ~= nil then LEVEL.Value = store:GetAsync(pKey, playerData["playerStats"]["Level"]) XP.Value = store:GetAsync(pKey, playerData["playerStats"]["XP"]) PVP.Value = store:GetAsync(pKey, playerData["playerStats"]["PvPPoints"]) VIP.Value = store:GetAsync(pKey, playerData["playerPasses"]["VIP"]) BSB.Value = store:GetAsync(pKey, playerData["playerPasses"]["BSB"]) HTTYD.Value = store:GetAsync(pKey, playerData["playerPasses"]["HTTYD"]) print("Values loaded successfully!") end while true do playerData = { ["playerStats"] = { ["Level"] = plr.playerStats.Level.Value, ["XP"] = plr.playerStats.XP.Value, ["PvPPoints"] = plr.playerStats.PVP.Value }, ["playerPasses"] = { ["VIP"] = plr.playerStats.VIP.Value, ["BSB"] = plr.playerStats.BSB.Value, ["HTTYD"] = plr.playerStats.HTTYD.Value } } store:SetAsync(pKey, playerData) wait() end end)
|
|
|
| Report Abuse |
|
|
|
| 02 Jan 2015 06:06 PM |
| GetAsync takes one argument, not two. |
|
|
| Report Abuse |
|
|
| |
|
| |
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
| |
|
|
| 02 Jan 2015 08:58 PM |
I've changed
if store:GetAsync(pKey, playerData) ~= nil then
to
if store:GetAsync(pKey) ~= nil then
Now, how would I recall the value of, for example, the level?
LEVEL.Value = ??? |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2015 08:58 PM |
| Plus, you don't need to do plr = player, you have already defined player and it doesnt look like you use plr anywhere |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2015 09:04 PM |
| Ahh, right. I forgot to remove that. I was messing around with the script earlier and I needed that to make something work. |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 02 Jan 2015 09:08 PM |
It looks like you are saving a table to that key, is this correct? That means GetAsync is returning a table, that should have "playerStats.Level" under it. Instead of caling GetAsync every time you want to look for that table, just put it in a variable.
local playerData = store:GetAsync(pKey) if playerData ~= nil then LEVEL.Value = store:GetAsync(pKey, playerData["playerStats"]["Level"]) XP.Value = store:GetAsync(pKey, playerData["playerStats"]["XP"]) PVP.Value = store:GetAsync(pKey, playerData["playerStats"]["PvPPoints"]) VIP.Value = store:GetAsync(pKey, playerData["playerPasses"]["VIP"]) BSB.Value = store:GetAsync(pKey, playerData["playerPasses"]["BSB"]) HTTYD.Value = store:GetAsync(pKey, playerData["playerPasses"]["HTTYD"]) print("Values loaded successfully!") end
Should be good |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2015 09:42 PM |
Yes. The table I'm saving is called 'playerData' to the key (ID of the player). As for the code you gave me, it's still not loading the data. Here's what the script looks like so far. Also, the table is located at the bottom, inside the loop, if you want to take a loot at that.
store = game:GetService("DataStoreService"):GetDataStore("GameDataTest")
game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady() pKey = player.userId ps = Instance.new("IntValue", player) ps.Name = "playerStats" LEVEL, XP, PVP, VIP, BSB, HTTYD = Instance.new("IntValue", ps), Instance.new("IntValue", ps), Instance.new("IntValue", ps), Instance.new("BoolValue", ps), Instance.new("BoolValue", ps), Instance.new("BoolValue", ps) LEVEL.Name = "Level" XP.Name = "XP" PVP.Name = "PVP" VIP.Name = "VIP" BSB.Name = "BSB" HTTYD.Name = "HTTYD"
local playerData = store:GetAsync(pKey) if playerData ~= nil then LEVEL.Value = store:GetAsync(pKey, playerData["playerStats"]["Level"]) XP.Value = store:GetAsync(pKey,playerData["playerStats"]["XP"]) PVP.Value = store:GetAsync(pKey,playerData["playerStats"]["PvPPoints"]) VIP.Value = store:GetAsync(pKey,playerData["playerPasses"]["VIP"]) BSB.Value = store:GetAsync(pKey, playerData["playerPasses"]["BSB"]) HTTYD.Value = store:GetAsync(pKey,playerData["playerPasses"]["HTTYD"]) print("Values loaded successfully!") end while true do playerData = { ["playerStats"] = { ["Level"] = player.playerStats.Level.Value, ["XP"] = player.playerStats.XP.Value, ["PvPPoints"] = player.playerStats.PVP.Value }, ["playerPasses"] = { ["VIP"] = player.playerStats.VIP.Value, ["BSB"] = player.playerStats.BSB.Value, ["HTTYD"] = player.playerStats.HTTYD.Value } } store:SetAsync(pKey, playerData) wait() end end)
|
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 02 Jan 2015 09:47 PM |
You're still misusing GetAsync by passing it more than one argument and you are using GetAsync more than once even though I told you to make a variable so you don't have to do that.
local playerData = store:GetAsync(pKey) if playerData ~= nil then LEVEL.Value = playerData.playerStats.Level XP.Value = playerData.playerStats.XP PVP.Value = playerData.playerStats.PvPPoints VIP.Value = playerData.playerStats.VIP BSB.Value = playerData.playerStats.BSB HTTYD.Value = playerData.playerStats.HTTYD print("Values loaded successfully!") end |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 02 Jan 2015 09:50 PM |
Also this will error while true do playerData = { ["playerStats"] = { ["Level"] = player.playerStats.Level.Value, ["XP"] = player.playerStats.XP.Value, ["PvPPoints"] = player.playerStats.PVP.Value }, ["playerPasses"] = { ["VIP"] = player.playerStats.VIP.Value, ["BSB"] = player.playerStats.BSB.Value, ["HTTYD"] = player.playerStats.HTTYD.Value } } store:SetAsync(pKey, playerData) wait() end You can't save that many times. Only save them on the changed event, so do something like this
function changed() playerData = { ["playerStats"] = { ["Level"] = player.playerStats.Level.Value, ["XP"] = player.playerStats.XP.Value, ["PvPPoints"] = player.playerStats.PVP.Value }, ["playerPasses"] = { ["VIP"] = player.playerStats.VIP.Value, ["BSB"] = player.playerStats.BSB.Value, ["HTTYD"] = player.playerStats.HTTYD.Value } } store:SetAsync(pKey, playerData)
player.playerStats.Level.Changed:connect(changed) player.playerStats.XP.Changed:connect(changed) etc,
also you might want to add a debounce and a small wait after setting the debounce to true in the changed event, because I assume when level changes XP will two and it will save twice when it only needs to save once. |
|
|
| Report Abuse |
|
|
|
| 02 Jan 2015 10:02 PM |
| Thank you very much, lordrambo! I touched up everything you suggested and the script works just fine now; all values are loaded no problem. Cheers to you! |
|
|
| Report Abuse |
|
|