Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Whats wrong with my admin Commands?

Asked by
IcyEvil 260 Moderation Voter
10 years ago

Im trying to make a kill command and it isn't working...

Please Help...

local owners = {"EmperorVolvax"}

game.Players.PlayerAdded:connect(function(p)
for _,v in pairs(owners) do
if p.Name == v then
p.Chatted:connect(function(msg)
if msg == "#kill " then
local vic = msg:sub(1, 6)
if vic then
vic.Character.Humanoid:TakeDamage(100)
end
end
end)        
end
end
end)
0
(I edited my post) Perci1 4988 — 10y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Well you first problem is that nothing is tabbed correctly. This makes it more difficult to read and, as a result, more difficult to debug. This is why readability is arguably even more important than functionality. So let's tab this correctly.

local owners = {"EmperorVolvax"}

game.Players.PlayerAdded:connect(function(p)
    for _,v in pairs(owners) do
        if p.Name == v then
            p.Chatted:connect(function(msg)
                if msg == "#kill " then
                    local vic = msg:sub(1, 6)
                    if vic then
                        vic.Character.Humanoid:TakeDamage(100)
                    end
                end
            end)        
        end
    end
end)

Your functionality problem is the fact that is appears you don't fully understand substrings. :sub(), or string.sub(), will take the string given and return a new string equal to the characters between the two numbers given. You can easily understand this by experimenting and simply counting the characters. Take a look at the following examples.

local myString = "Hello, Mom!"
print(myString:sub(2, 5))
    --ello
print(myString:sub(3, 7))
    --llo, M (Spaces count!)
print(myString:sub(6))
    --, Mom!
    --When only a single argument is given it returns the substring between that number and the total number of characters in the string, so it goes from 6 to the end.

Let's pretend someone were to say, "#kill Perci1". The script would never pass the if statement on line 07 because the messsage isn't equal to "#kill ".

But, hypothetically, if the script were to somehow pass the if statement on line 07, what would the substring between the first character and the sixth character equal? If you count, you'll see that it equals "#kill ". Therefore, vic = "#kill "

vic is in no way equal to a Player object that we can access the Character from.


To solve this, we must get all the characters after the substring "#kill ". We must do this because if I were to say "#kill Perci1" the name of the person I want dead is everything after "#kill ".

We must get the seventh character and everything beyond the seventh character, so we must do :sub(7)

We can use the FindFirstChild method to make sure a true Player name was spoken.


local owners = {"EmperorVolvax"}

game.Players.PlayerAdded:connect(function(plr)
    for _,v in pairs(owners) do
        if plr.Name == v then
            plr.Chatted:connect(function(msg)
                if msg:sub(1, 6) == "#kill " then
                    local vic = game.Players:FindFirstChild(msg:sub(7))
                    if vic and vic.Character then
                        vic.Character:WaitForChild("Humanoid").Health = 0
                    end
                end
            end)        
        end
    end
end)

EDIT: You would have to use :match() or string.match().

What this does is literally try to "match" the string given in the parentheses to the string it was called on.

local myString = "Hello"

print(myString:match("el"))
    --el

print(myString:match("12345"))
    --nil

You can now simply loop through Players and check if a small string can be matched to their entire name.

for i, player in pairs(game.Players:GetPlayers()) do
    if player.Name:match("Bob") then
        print("It matched.")
    end
end

Just note that capitals will still matter, if you want them to not matter you just need to use :lower() or string.lower() to convert everything to lower case.

0
How would you make it to where it would be like #kill emp or #kill emperor instead of #kill EmperorVolvax? IcyEvil 260 — 10y
Ad

Answer this question