Admin command not working?

I’m trying to make my own admin command, but when I type in ‘/fog 100’ in the chat nothing happens with no errors, I’m trying to get the number after ‘/fog’ and make the FogEnd that.

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if string.sub(message:lower(), 1, 8) == "/fog" then
			local FogValue = string.sub(message:lower(), 7)
			game.Lighting.FogEnd = FogValue
		end			
	end)
end)

Can you try printing something to check if the command works or not?

The issue is here. You would want to check if 1, 4) == "/fog"

I suggest instead of using a sub like this for every command, you split the message into an array and have callback functions in a dictionary.

Fast code I wrote to illustrate the idea.

local commands = {
	fog = function(value)
		value = tonumber(value)
		
		game.Lighting.FogEnd = value
	end
}

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message:sub(1, 1) == "/" then
			message = message:gsub("/", "")
			
			local arguments = message:split(" ")
			local command = table.remove(arguments, 1)
			
			local callback = commands[command]
			
			if (callback) then
				callback(unpack(arguments))
			end
		end		
	end)
end)

Try reading this devforum post for more: How to make basic admin commands

1 Like