Need help fixing my custom admin Commands

Trying to create my own admin commands. Not sure how to get a number out from a string and then use that for example, heal the player or damage the player. I’m wanting to make most basic things admin commands would have, then make my own antiexploit.

If anyone can help, would be much appreciated. Thanks in advance.

local admins = {"KryptonFoox"}

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		for _,a in ipairs(admins) do
			if plr.Name == a then
				if string.match(msg:lower(),'/kill') then
					local player = msg:sub(7)
					game.Players[tostring(player)].Character.Humanoid.Health = 0
				elseif string.match(msg:lower(),'/god') then
						local player = msg:sub(6)
					game.Players[tostring(player)].Character.Humanoid.Health = math.huge
					game.Players[tostring(player)].Character.Humanoid.MaxHealth = math.huge
				elseif string.match(msg:lower(),'/heal') then
				local player = msg:sub(5)
					local toRemove = string.len(player)
					local hpToGive = string.match(player,"%d")
					print(hpToGive)
				end
			end
		end
	end)
end)

You just need to convert it to a number using tonumber()

Script:

local hpToGive = tonumber(string.match(player,"%d"))

2 Likes

Alright. Will try this out, if it works or not. If it does, I’ll be using it for most of the number stuff.

Do what @Bnxdit said also add an if statement with typeof checking if it’s actually a number and not nil or anything.

1 Like

How would I use typeof? Don’t know much of those things xD

local num = tonumber(StringNumber)
if typeof(num) == "number" then
    --do stuff
end
2 Likes

Have you tried adding prints() to see which line isn’t working?

On a side note

Just some tips for future reference:
use pairs because ipairs is slow.
You don’t need to convert the player argument to a string because it already is one. Even with numbers in it it is typed in chat so everything is already in string format.

1 Like

Besides what @uhi_o did (which is great), you can view Type() vs typeof()? which speaks about the same thing and differences.

2 Likes

Yeah I’ve tried printing. It only posts 1 on the tonumber thing.

Also, don’t run the code through the for loop. The more admins there are, the more time the code runs do something like this:

local isAdmin = false

for _,v in pairs(admins) do
	if player.Name == v then
		isAdmin = true
	end
end

if isAdmin == true then
	-- Run code
end
1 Like

Will try that soon. I’m just trying to now get the players name by matching it via %a.

local player = msg:sub(6):match(“%a”) is how i’m doing it by the way. Outputs just K. I’ll try what you said earlier though to fix it.

To correct my self I would say the new Roblox Lua VM fixed the slowness if ipairs I think so just keep on using it but it only really makes sense to use it when you have like a mixed table but I was wrong.

1 Like

In addition, I would suggest using the UserId of the admins instead of their names in case they change their names and I believe there is an easier and shorter way to do the same thing.

local Admins = {UserId, UserId}

local FindPlrInAdmins = table.find(Admins, plr.UserId)
if FindPlrInAdmins ~= nil then
-- Script
end
2 Likes

Never ever use names for validation, names can be changed and replaced. Instead, use userids these cant be changed!

Also, it seems that you’re setting the health of the Humanoid before the max health in the /god command.

1 Like