Console Commands to Run Serverside

I’m attempting to create console commands to run functions server side, for the time being for admin controls or other fun bits. The problem is that console commands seem to be only run by the client (or if through the server console itself) by that client only. I’m trying to tell the server to run a function when the client runs that particular command.

The code itself looks like this, situated in addons on the server.


local function CCRespawn(ply, cmd, arg)
	
	local tar = arg[1]
	if (tar == nil) then tar = ply:Nick() end
	
	if (tar != "*") then
		local ts = {}
		local ps = player.GetAll()
		for k,v in pairs(ps) do
			if (string.find(v:Nick(), tar)) then table.insert(ts, v) end
		end
		
		if (table.Count(ts) < 1) then
			ply:ChatPrint("!respawn:	Player not found.")
		elseif (table.Count(ts) > 1) then
			ply:ChatPrint("!respawn:	Name too generic, found more than one matching player.")
		else
			tar = ts[1]
			tar:SetHealth(tar:GetMaxHealth())
			tar:UnSpectate()
			tar:SetTeam(1)
			tar:Spawn()
			ply:ChatPrint("!respawn:	Respawned " .. tar:Nick())
		end
	end
end

concommand.Add("!respawn", CCRespawn)

However, when the client runs !respawn the client attempts to run the script itself, and without permissions to run GetMaxHealth, UnSpectate, etc, it just errors out.

I realize there is ULX for this, but I’m trying to figure out how this works before I go and take the easy route never to learn anything.

Make sure the file is not being sent to the client. Even though you are sending it to the client it should not prevent it from working.
I am also a bit unsure if the ‘!’ is not an illegal character in console commands. I may be wrong, just I have never used it in any of my commands.

If you have any errors, they would be of much help.

How would I go about /not/ sending this file to the client? I’ve tried placing if (CLIENT) then return end before everything, but this simply just makes the client’s script not run at all, regardless of its placement.

The script doesn’t have any errors otherwise. The server doesn’t act unless I run it through the console on the server, in which case it works as intended. On the client it dies at GetMaxHealth because it doesn’t have access to that function (my assumption).

I am assuming you are already sending it to the client. If you put the ‘If !CLIENT then return end’ at the top of the file, this would only prevent the client from running the code.
This is strange. Adding a command on the server should give clients access to it. Have you posted the whole file in the OP?

It is not a case of the client not having access to a function through privileges, merely the fact that the function does not exist on the client. The fact you know it dies at that line on the client implies it errors.

Try not running the code on the client. At a guess I would say that if a command function exists on the client, it will not be networked to the server. And in that case it would explain why it does not work.

Yes the client is receiving the file too, which it probably shouldn’t. Currently the script is in the directory addons/ConsoleCommmands/lua/autorun/init.lua
Where should I place it so that it doesn’t send it to the client as well?

Place it in addons/ConsoleCommands/lua/autorun/server/init.lua (If this is the only command in there, an init file usually contains more than one command, so don’t go breaking things, create a new file and remove the command from the old one.)

Ah splendid, that did the trick, thank you very much sir.