Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I called a function with a string parameter and the function received a empty table argument?

So I'm making my own fully customization, easy to edit admin commands and system goes like this:

  • Whenever the client chats it sends the server what it chatted
  • The server checks if the client is an admin or not
  • The server then slices the chat message into spaces e,g ( "/kick me" --> {"/kick", "me"} )
  • The server identifies the command, the person to be executed on

Everything works great, getting the everyone command and others command and the me command are working fine since they don't need the player's chat message to determine who to execute a command on.

Except for the commands that are executing on a team or on a certain player. Getting that certain team or player requires the player's chat message. My solution was to feed it into a function and find the team or player there. Instead it gave me this error at me and I don't know why.

Unable to cast value to std::string

Now I do print debugging to find out what values am I passing and throwing around and it looks like if I call a function with the player's chat message cut into words the function receives an empty table, very confusing.

Here's my snippet for taking care of the chat message to commands for players part: In this scenario the admin said: /kick 123nabilben123

else
	print(ChatSpliced[2]) --> string "123nabilben123"
	Objection = GetFunctions:Person(ChatSpliced[2])
end

Now here's the snippet that is in a module script receiving that function call:

Person = function(ChatMessage) -- executing on a certain player -- in a module script 
	print(ChatMessage) -- table hexadecimal address 
	if PlayersService:FindFirstChild(ChatMessage) then
		return PlayersService:FindFirstChild(ChatMessage)
	else
		return nil
	end
end

The reason why ChatSpliced was in a table is because I used string.gmatch() and the string pattern "%S+" to separate the string into smaller strings by spaces.

It's very confusing and I don't know why the chat message turns into a table. There's nothing changing the string at all and the function's arguments. Sorry if this seems like a lengthy question but I am seriously confused.

1 answer

✓ Accepted answer

Executing a function from a table with a : means the first argument is the table itself and the parameters kinda shifts to the right by one.

Here's an example, notice parameter self

local table_1 = {
	Function1 = function()
		print'Wow print';
	end;
	Function2 = function(self,a)
		if a==2 then
			self.Function1();
		else
			print'nothing'
		end;
	end;
}
table_1:Function2(2)--> 'Wow print' since it goes to the second argument
table_1.Function2(2)--> 'nothing' since its the first argument

1 comment

0

Thank you for helping me

This discussion is preserved as it was. New answers, comments, and votes are closed.