Error message: "attempt to perform arithmetic (mul) on number and table"

Hey i’m trying to make a global leaderboard but i keep getting an warned errormessage.

local DataStoreService = game:GetService("DataStoreService")
local RocksLeaderboard = DataStoreService:GetOrderedDataStore("RocksLeaderBoard")

local function UpdateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = RocksLeaderboard:GetSortedAsync(false, 5)
		local RocksPage = Data:GetCurrentPage()
		for _, data in ipairs(RocksPage) do
			local Username = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = Username
			local Rock = data.value
			local IsOnLeaderboard = false
			for _, v in pairs(game.Workspace.LeaderBoard.SurfaceGui.Values:GetChildren()) do
				if v.Player.Text == Name then
					IsOnLeaderboard = true
					break
				end
			end
			
			if Rock and IsOnLeaderboard == false then
				local newLeaderBoardFrame = game.ReplicatedStorage:WaitForChild("LeaderBoardFrame"):Clone()
				newLeaderBoardFrame.Player.Text = Name
				newLeaderBoardFrame.Rocks.Text = Rock
				newLeaderBoardFrame.Position = UDim2.new(0,0, newLeaderBoardFrame.Position.Y.Scale + (.08 * game.Workspace.LeaderBoard.SurfaceGui.Values:GetChildren()), 0)
				newLeaderBoardFrame.Parent = game.Workspace.LeaderBoard.SurfaceGui.Values
			end
		end
	end)
	
	if not success then
		warn(errorMessage)
	end
end

while true do
	for _, Player in pairs(game.Players:GetPlayers()) do
		RocksLeaderboard:SetAsync(Player.UserId, Player.leaderstats.Rocks.Value)
	end
	for _, v in pairs(game.Workspace.LeaderBoard.SurfaceGui.Values:GetChildren()) do
		v:Destroy()
	end
	UpdateLeaderboard()
	wait(10)
end

That was my code.
image
And this is my error message.

game.Workspace.LeaderBoard.SurfaceGui.Values:GetChildren()
is the problem.
Get children returns a table containing every children of an instance.

2 Likes

It still didn’t work to do what you said.

Which line is 36?

Also you’re gonna run into throttling issues if you save data every 10 seconds.

ik it’s for testing. I’m keeping it 10 sec beacuse then i don’t need to wait to check if it works. And it’s line 31 that’s warning the error and line 10 that’s making the error

The error occurs due to this line:

.08 * game.Workspace.LeaderBoard.SurfaceGui.Values:GetChildren()

Basically you’re trying to multiply 0.08 with the table itself not the table length, which isn’t possible. To get the table length you must use the # symbol:

local array = workspace.LeaderBoard.SurfaceGui.Values:GetChildren()
local length = #array
newLeaderBoardFrame.Position = UDim2.new(0,0, newLeaderBoardFrame.Position.Y.Scale + .08*length, 0)
2 Likes

Hey thank you for helping me but i need help with one last thing. That is my new error “Players:GetUserIdFromNameAsync() failed: Unknown user”

tonumber(data.key) is their UserId, I assume you meant Players:GetNameFromUserIdAsync(tonumber(data.key))?

It’s okay i found out how to make it work. I had to make a new DataStore since my datastore was full or somthing since it was on a 10 seconds refresh cooldown

It may be due to the Player1, Player2, Player3 dummy accounts, since their UserIds don’t really exist. To avoid this issue add this scenario to your code:

for _, Player in pairs(game.Players:GetPlayers()) do
	if Player.UserId > 0 then --dummy players have negative UserIds
		RocksLeaderboard:SetAsync(Player.UserId, Player.leaderstats.Rocks.Value)
	end
end

Yeah but the error also shows when I join the game. But it’s fine now i don’t really need to worry to much about the error since it was just a youtube tutorial. And i have a question but i will ask you in DM’s