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

My script keep looping code?

Asked by 8 years ago

Okay so I have people on my game and I gave them perm admin. I wanna restrict what they can do so I made a script of different commands NO ONE except for people I put is allowed, here is the script Problem below the script!

Oh yes, and if an admin sees this (I am fireboltofdeath) can you please tell me why my fireboltofdeath account got suspended?

fulladmins = {"fireboltofdeath","adorablegirl1998"}

Restricted = {":give","","","","","","","","","","","","","","","","","","","","","","","",""}


game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        for i = 1,#fulladmins do    
    if p.Name ~= fulladmins[i] then
    for z = 1,#Restricted do if string.find(string.lower(c),string.lower(Restricted[z])) then
        m = Instance.new("Message", workspace)
        m.Text = "ALERT! ALERT!"
        wait(1.5)
        m.Text = "Access Restricted, kicking player: " .. p.Name
        wait(0.5)
        p:Kick()
        wait(1.5)
        m.Text = "Restricted Command Used: " .. c
        wait(1.5)
        m:Destroy()
        script.Disabled = true
        script.Disabled = false
    end 
    end
    end
    end
    end)
    end)

Script works okay, but when I say :give (Testing command) it does the code but it loops the same code (No idea how many times since I leave right after to test)

0
And it says no errors in the output. Every single time I checked mechmasterfire 50 — 8y

2 answers

Log in to vote
1
Answered by
Azmidium 388 Moderation Voter
8 years ago

The issue is your table has a ton of children. (The blank quotes) It is looping because the #Retricted is all of those children, so it runs that many times. I reccomend you remove those.

Basically change this:

Restricted = {":give","","","","","","","","","","","","","","","","","","","","","","","",""}

To this:

Restricted = {":give"}

EDIT: Another reason for this looping is that you also loop through the admins. If you need that check you can simpily add a function to check for you.

function checkIfItemInTable(aTable, item)
    local itemIn = false
    for i, v in pairs (aTable) do
        if v == item then
            itemIn = true
        end
    end
    return itemIn
end

You can plug this function in and do this:

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
    if checkIfItemInTable(fulladmins, p.Name) then
    for z = 1,#Restricted do if string.find(string.lower(c),string.lower(Restricted[z])) then
        m = Instance.new("Message", workspace)
        m.Text = "ALERT! ALERT!"
        wait(1.5)
        m.Text = "Access Restricted, kicking player: " .. p.Name
        wait(0.5)
        p:Kick()
        wait(1.5)
        m.Text = "Restricted Command Used: " .. c
        wait(1.5)
        m:Destroy()
        script.Disabled = true
        script.Disabled = false
    end 
    end
    end
    end)
    end)

What the function does is it loops through the table to see if the item is in it. If it finds it, it will be true. The if statement needs to be true to continue, so if you plug in the right variables it will check if p.Nameis in fulladmins END OF EDIT

I hope this helped!

Sincerely, jmanrock123

0
Okay but after I add more, it will still loop? Anhhh I'll just test it real quick. mechmasterfire 50 — 8y
0
No it will run that many times because each cama adds 1 to the Restricted count. Azmidium 388 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

The issue is that you have all of those blank strings ("") in your restricted table. The absence of a character will make string.find true for any string if you try to find a blank string.

Another issue is this bit:

if p.Name ~= fulladmins[i] then

What this does is check if the player who chatted's name isn't equivalent to the current fulladmin that's being "viewed" by the for loop. However, what if the admin is in the admins list, but hasn't been iterated yet?

Here's a fixed version of your script;

local admins = {"fireboltofdeath","adorablegirl1998"}

local Restricted = {":give"}

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(c)
        local isAdmin = false

        for _, admin in pairs(admins) do
            if p.Name == admin then
                isAdmin = true
                break
            end
        end

        if not isAdmin then
            for _, restricted in ipairs(Restricted) do
                if string.find(string.lower(c), string.lower(restricted)) then
                    spawn(function()
                        m = Instance.new("Message", workspace)
                        m.Text = "ALERT! ALERT!"
                        wait(1.5)
                        m.Text = "Access Restricted, kicking player: " .. p.Name
                        wait(0.5)
                        p:Kick()
                        wait(1.5)
                        m.Text = "Restricted Command Used: " .. restricted
                        wait(1.5)
                        m:Destroy()
                    end)

                    break
                end
            end
        end
    end)
end)

Here's what changed: First, we removed all of the extra contents of Restricted. We also made Restricted a local variable, which is a good practice.

Next, rather than iterating through the admins in an incorrect way, we do this...

local isAdmin = false

for _, admin in pairs(admins) do
    if p.Name == admin then
        isAdmin = true
        break
    end
end

... to determine whether the player is an admin or not.

Then, we check here...

if not isAdmin then

... if they aren't an admin. If they aren't an admin, we proceed.

We then loop through the restricted words, checking if they're restricted. If the word is restricted, we do some magical stuff and use "break" to exit out of the for loop. We already kicked the player; there's no need to see if they used another restricted word.

A key change I made is that rather than using numerical iteration, where you loop through lists and get the value of the iteration using an index, we can simply iterate through the list without using numbers in this format:

for index, value in pairs(table) do
    -- example
end

Since we don't use the variable index in both of the cases where we use this type of for-loop, we just use the variable name _, which is conventional for an unused variable. We also name the value variable appropriately for whatever we're doing in the for loop.

Finally, note how in the restricted for loop, we use ipairs. ipairs functions just like pairs, except it makes sure to iterate through a table in the same order as the table was created.

Hope this helped. Good luck!

0
Okay I remove them but it still looped twice for the two things in the table (I added one more to test) mechmasterfire 50 — 8y
0
Okay I made it into one thing so it is Restricted = {":give"} but it still loops twice. mechmasterfire 50 — 8y

Answer this question