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

Does that even work? If so, I don’t know how.

I’ve told you about 3 times now, I did? Look at the code.

What is the line causing the error?

The line in the Local Script is:

script.Parent.Text = invoke[1].." players online"
1 Like

That is because (invoke) is a number, you are trying to index it like a table. Instead of invoke[1], just do invoke.

Or…

Something that would be better were to get rid of the .Value at the end of that first line, and instead of invoke[1], do invoke.Value.

1 Like

Now I’m getting the error:

attempt to concatenate nil with string

Also, the value is needed as the function requires a gameId.

1 Like

Ok, problem found.
Roblox thinks this:

is part of this:

Change the Event Script to this:

functions.GetPlayersOnline.OnServerInvoke = function(player, place_id)
    local Return
    local success, con = pcall(function()
	    MessagingService:SubscribeAsync("JoinEvent", function(message)
		    print("PlayerCount: "..message[1])
		    print("GameID: "..message[2])
		    Return = message
	    end)
    end)
    local success,Encode = pcall(function()
    	return HttpService:JSONEncode(Return)
    end)
	if success then
        return Encode
    else warn("Error")        end)
end
1 Like

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