Attempt to call table value

Hello everyone! I am making a simulator game and in my “server” script I keep getting this error:

ServerScriptService.GameServers.Server:30: attempt to call a table value

I’ve spent a while trying to figure it out but it doesn’t make sense to me. I am new to lua so any help is good.

Script:

local datastore = game:GetService(“DataStoreService”):GetDataStore(“Divine”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Parent = player
leaderstats.Name = “leaderstats”

local cash = Instance.new("IntValue")
cash.Parent = leaderstats
cash.Name = "Cash"

local buildpower = Instance.new("IntValue")
buildpower.Parent = leaderstats
buildpower.Name = "BuildPower"

local rebirths = Instance.new("IntValue")
rebirths.Parent = leaderstats
rebirths.Name = "Rebirths"

local debounce = Instance.new("BoolValue")
debounce.Value = false
debounce.Name = "Debounce"
debounce.Parent = player

local key = "user"..player.UserId

local storeditems = datastore:GetAsync(key)

if storeditems then
	cash.Value = storeditems(1)
	rebirths.Value = storeditems(2)
	buildpower.Value = storeditems(3)
else
	local items = {cash.Value, rebirths.Value, buildpower.Value}
	
	datastore:SetAsync(key, items)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local items = {player.leaderstats.Cash.Value, player.leaderstats.Rebirths.Value, player.leaderstats.BuildPower.Value}
end)

  • You save the data from the datastore into the datastore when the player joins, which is useless.
  • Player data not saved when player leaves.
  • You do not create new data if it is empty.
  • You don’t check if storeditems is a table.

This edition of the script would solve the problems that I have brought up above:

local datastore = game:GetService("DataStoreService"):GetDataStore("Divine")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Parent = leaderstats
	cash.Name = "Cash"

	local buildpower = Instance.new("IntValue")
	buildpower.Parent = leaderstats
	buildpower.Name = "BuildPower"

	local rebirths = Instance.new("IntValue")
	rebirths.Parent = leaderstats
	rebirths.Name = "Rebirths"

	local debounce = Instance.new("BoolValue")
	debounce.Value = false
	debounce.Name = "Debounce"
	debounce.Parent = player

	local key = "user"..player.UserId

	local storeditems = datastore:GetAsync(key)
	
	if not storeditems then
		cash.Value = 0
		rebirths.Value = 0
		buildpower.Value = 0
	end
	if storeditems then
		cash.Value = storeditems(1)
		rebirths.Value = storeditems(2)
		buildpower.Value = storeditems(3)
	else
		local items = {cash.Value, rebirths.Value, buildpower.Value}
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local items = {player.leaderstats.Cash.Value, player.leaderstats.Rebirths.Value, player.leaderstats.BuildPower.Value}
	datastore:SetAsync(player.UserId,items)
end)
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

Thank you! This worked perfectly, I was just following a tutorial and my internet is pretty bad so it was kinda blurry.

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.