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