Attempt to index nil or with number when attempting to get invoke[1]

This fixed the error, however, the text still says “? players online”.

#PlayerCount is #game:GetService(“Players”):GetPlayers()?
If not, what is its value?

That means that your value is nil. Also, you are just moving the value, so it will still work. So that means your invoke is nil.

Yes, it is players:GetPlayers().

If it is nil, it says “nil players online”. I know this because one time I tried to do this, it returned nil. The “? players online” is just a placeholder.

No, because you are trying to concatenate nil and a string. Allow me to explain concatenation. Concatenation is combining multiple values to form a string. If one of the values is nil, it will error. When you are doing …, that is concatenation. The first value is nil, and the second is a string. Your first value is invoke. Therefore, invoke is nil. The reason it says ?, is because it errors on that line, so the text does not get updated.

Also, the reason why invoke is nil is because when you return the two values in your function, you are returning it to the function hooked to your event. You are returning to the wrong function. Make sure you write your function outside of the function connected to the subscription event, so it is able to return a value.

I used the code from @SOTR654, now it only returns 1 thing, the player count. But, it still returns nil.

After reading this:

I think I should change the LocaleScript for this:

local HttpService = game:GetService("HttpService")
local invoke = game.ReplicatedStorage.Functions.GetPlayersOnline:InvokeServer(placeid.Value)
local success,Decode = pcall(function()
    return HttpService:JSONDecode(invoke)
end)
if success then
    script.Parent.Text = Decode[1] .." players online"
else warn("Error")    end)

And see my script, I will change it.
I hope it works xd

1 Like

Well if you use that code, then you are returning a table. In that case, you will want to do invoke[1].

Also @SOTR654 , it is better to tell him what is wrong rather than just giving him code without explaining what it does, or else he will not learn what he did wrong.

Also Http:JSONEncode() and :JSONDecode() can both error, so you would need to wrap it in a pcall.

Honestly, I’m still learning a lot about programming because I’m new and I speak Spanish, the google translator is my savior xd
And since I don’t know how to explain very well for the same reason, I explain when I can.

Now I change it.

1 Like