if storeditems then
cash.Value = storeditems(1)
rebirths.Value = storeditems(2)
buildpower.Value = storeditems(3)
else
This is not how you index tables, this is why your are getting the error attempting to call a table value, the table is not a function. You can either do
if storeditems then
cash.Value = storeditems[1]
rebirths.Value = storeditems[2]
buildpower.Value = storeditems[2]
else
or if you want to save it in a dictionary
if storeditems then
cash.Value = storeditems.Cash
rebirths.Value = storeditems.rebirths
buildpower.Value = storeditems.buildpower
else
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")
local ProtectedCall = pcall
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 BuildPower = Instance.new("IntValue")
BuildPower.Name = "BuildPower"
BuildPower.Parent = Leaderstats
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = Leaderstats
local Debounce = Instance.new("BoolValue")
Debounce.Name = "Debounce"
Debounce.Parent = Player
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync("Data_"..Player.UserId)
end)
if Success then
if Result then
if type(Result) == "table" then
Cash.Value = Result[1]
BuildPower.Value = Result[2]
Rebirths.Value = Result[3]
Debounce.Value = Result[4]
end
end
else
warn(Result)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync("Data_"..Player.UserId, {Player.leaderstats.Cash.Value, Player.leaderstats.Rebirths.Value, Player.leaderstats.BuildPower.Value, Player.Debounce.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync("Data_"..Player.UserId, {Player.leaderstats.Cash.Value, Player.leaderstats.Rebirths.Value, Player.leaderstats.BuildPower.Value, Player.Debounce.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end
end)
Here’s an improved version which also saves/loads the value of “Debounce” as well.