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

Hello,

I’m currently trying to get the player count for a different place using MessagingService, however, I’m getting the error “attempt to index nil or with number”. Here’s my code:

Local Script:

local invoke = game.ReplicatedStorage.Functions.GetPlayersOnline:InvokeServer(placeid.Value)

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

Event Script:

functions.GetPlayersOnline.OnServerInvoke = function(player, place_id)
	local success, con = pcall(function()
		MessagingService:SubscribeAsync("JoinEvent", function(message)
			print("PlayerCount: "..message[1])
			print("GameID: "..message[2])
			
			return message[1], message[2]
		end)
	end)
end

Script in different place:

Players.PlayerAdded:Connect(function(player)
	local success, result = pcall(function()
		local message = {#PlayerCount, game.PlaceId}
		MessagingService:PublishAsync("JoinEvent", message)
	end)
	
	if not success then
		print(result)
	end
end)

Any idea how to solve this? If so, please post it below.

Thanks,
p0s_0.

2 Likes

functions.GetPlayersOnline.OnServerInvoke = function(player, place_id)
local success, con = pcall(function()
MessagingService:SubscribeAsync(“JoinEvent”, function(message)
print("PlayerCount: "…message[type any number here])
print("GameID: "…message[put function 2])

		return message[type message hhere], message[function]
	end)
end)

end


Script in different place:

Players.PlayerAdded:Connect(function(player)
local success, result = pcall(function()
local message = {#PlayerCount, game.PlaceId}
MessagingService:Publish(fuction(“JoinEvent”, message)
end)

if not success then
	print(result)
end

end)

[/quote]
put in ScriptStorage. and add events to ReplicatedStorage.
1 Like

Uhhhhhh, what? I don’t know. Can you format it?

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

I don’t know what you’re trying to say here. Also, please format your code properly.

This post was flagged by the community and is temporarily hidden.

That’s what I do in the local script whenever I’m firing the remote function.

This post was flagged by the community and is temporarily hidden.

I’m typing the game id though?

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

This post was flagged by the community and is temporarily hidden.

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