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

SetAsync / DataStoreService not working with table?

Asked by 3 years ago

Gives me an error with "ServerScriptService.DataStore:33: attempt to index number with 'cash'


local DataStoreService = game:GetService("DataStoreService") local cashStore = DataStoreService:GetDataStore("PlayerCash") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = leaderstats local Wins = Instance.new("IntValue") Cash.Name = "Wins" Cash.Parent = leaderstats local playerUserId = player.UserId local data local succ, err = pcall(function() data = cashStore:GetAsync("Player_"..playerUserId) end) if succ then Cash.Value = data.cash Wins.Value = data.wins elseif err then print(err) end end) game.Players.PlayerRemoving:Connect(function(player) local playerUserId = player.UserId local storedIntegers = { cash = player.leaderstats.Cash.Value; wins = player.leaderstats.Wins.Value; } local succ, succ = pcall(function() cashStore:SetAsync("Player_"..playerUserId,storedIntegers) end) end)
0
I think you have to put Cash.Value = data.Cash Wins.Value = data.Wins instead of Cash.Value = data.cash Wins.Value = data.wins but i'm not sure Punctist 120 — 3y
0
False, i have two variables inside the table that are called "cash", "wins", which i'm trying to access from when the pcall is called success. ZOOP1015 44 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

[SOLVED]

Seems to be that everything worked fine, but for some reason, I had to add an if statement to if the data exists.

REVAMPED CODE :


local DataStoreService = game:GetService("DataStoreService") local cashStore = DataStoreService:GetDataStore("PlayerIntegerSave") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Cash = Instance.new("IntValue") Cash.Name = "Cash" Cash.Parent = leaderstats local Wins = Instance.new("IntValue") Wins.Name = "Wins" Wins.Parent = leaderstats local playerUserId = player.UserId local data local succ, err = pcall(function() data = cashStore:GetAsync("Player_"..playerUserId) end) if succ then if data then Cash.Value = data.cash Wins.Value = data.wins end elseif err then print(err) end end) game.Players.PlayerRemoving:Connect(function(player) local playerUserId = player.UserId local data = { cash = player.leaderstats.Cash.Value; wins = player.leaderstats.Wins.Value; } local succ, err = pcall(function() cashStore:SetAsync("Player_"..playerUserId,data) end) end)
Ad

Answer this question