Server script: attempt to perform arithmetic (add) on boolean and number

Hello! :slightly_smiling_face:

I want to fix the game counter error I can’t find a fix for.
The variable ‘player_flags’ is a table that has an empty nested table inside named ‘game_counter’:

local player_flags = {
   game_counter = {}
}

The problematic line of code: (edit: it’s under a for loop)

player_flags.game_counter[plr.UserId] += 1

Part of the PlayerAdded function:

player_flags.game_counter[player.UserId] = 0

Thanks in advance!

The error does not coincide with the exerpts you’ve given us. Somewhere in you’re script, you must be overriding this number to a boolean

1 Like

I used the ctrl + f shortcut and didn’t find any booleans for game_counter

Perhaps you should make sure PlayerAdded runs before running the rest of the code

player_flags.game_counter[plr.UserId]

probably equals nil which cause you to get an error

** to test do print(player_flags.game_counter[plr.UserId]) and see what it prints

1 Like

I did this in PlayerAdded:

print("a player just joined!")
if player_flags.game_counter[player.UserId] then
	print(player_flags.game_counter[player.UserId])
end

It outputs: a player just joined! - Server - main:321 0 - Server - main:329

what is the error you are getting from the problematic code?

1 Like

it’s this: ServerScriptService.main:1012: attempt to perform arithmetic (add) on boolean and number

Try changing [plr.UserId] in the problematic code to [player.UserId]

1 Like

the 1st parameter name is ‘player’

Then I think you are overriding the number somewhere in the code. It would be helpful if you provide the full script somehow

1 Like

here:

local player_flags = {
    game_counter = {}
}


game.Players.PlayerAdded:Connect(function(player: Player)
    print("a player just joined!")

    player_flags.game_counter[player.UserId] = 0

    if player_flags.game_counter[player.UserId] then
        print(player_flags.game_counter[player.UserId])
    end
end)


function main()
    -- the part
    for _, plr in ipairs(game.Players:GetPlayers()) do
        player_flags.game_counter[plr.UserId] += 1
    end
end

tested it and it seems main function might be running before player connects. try adding task.delay

task.delay(1, main)
1 Like

3 second delay was the right amount. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.