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

Why can I still use "commands" when i'm not in the admins table?

Asked by 9 years ago

I don't know why I still can use ;kill Player when Player isn't in admins.

Script:

admins = {"d"}

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

game.Players.PlayerAdded:connect(function(plr)
    for i,v in pairs(admins) do
        plr.Chatted:connect(function(msg)
          if msg:sub(1,5) == ";kill" then
             victim = findPlayer(msg:sub(7))
             if victim and victim.Character then
                victim.Character:BreakJoints()
                end
            end
        end)
    end
end)
0
You only set it to loop through the table, while at the same time creating multiple anonymous for how many loops it went though! Consider using the 'if' statement? TheeDeathCaster 2368 — 9y
0
NEVERMIND! I can be so stupid somethimes, thx for reminding me. Operation_Meme 890 — 9y

2 answers

Log in to vote
4
Answered by 9 years ago

The reason is because you never actually checked if they are in the admin array.

All you did was:

for i,v in pairs(admins) do
plr.Chatted:connect(function(msg)
--blah
end)
end

In for i,v in pairs(admins) do you are merely only iterating through the table, but not even giving the script anything to do for each item in the table.

That's like saying in real life:

For everyone here.

Alright. Great. You've told us that the subject is 'Everyone' but you haven't told us what to do with the subject.

If you are making this an admin system, then you can change your code to the following:

admins = {"d"}

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function isAdmin(plr)
for _,v in pairs(admins) do
if v:lower() == plr.Name:lower() then
return true
end
end
return false
end

game.Players.PlayerAdded:connect(function(plr)
   if isAdmin(plr) then
        plr.Chatted:connect(function(msg)
          if msg:sub(1,5) == ";kill" then
             victim = findPlayer(msg:sub(7))
             if victim and victim.Character then
                victim.Character:BreakJoints()
            end
          end
      end)
   end
end)

0
I figured out i needed a if statment right before u posted the answer lol. thx Operation_Meme 890 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You must check to see if the player is in the admin list BEFORE you decide to listen to what they say

game.Players.PlayerAdded:connect(function(plr)
    for key, value in pairs(admins) do
        if value == plr.Name then --EDIT
            plr.Chatted:connect(function(msg)
                if msg:sub(1,5) == ";kill" then
                    victim = findPlayer(msg:sub(7))
                    if victim and victim.Character then
                        victim.Character:BreakJoints()
                    end
                end
            end)
        end 
    end
end)

Answer this question