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

Authorization script, it not works for both name why?

Asked by 7 years ago

When the player named "DAHUI84" enters the game, then it is automatically kicked, while the player named "NiniBlackJackQc" enters without problem. But i want both names to work, Can you help me?

local Authorization = {"NiniBlackJackQc","DAHUI84"}

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character

local Character = player.Character

for i, v in pairs(Authorization) do
    if player.Name == v or player.UserId == game.CreatorId then
        script.Parent:Destroy()
        GameSound()
        print("Administrator: "..player.Name, "Administrator Id: "..player.UserId)
        return; -- This stops the loop from running anymore.
    else
        Character:Destroy()
        Instance.new("BlurEffect",workspace.CurrentCamera)
        IntroSound()
        print("Player: "..player.Name, "Player Id: "..player.UserId)
        return;
    end
end
0
i think line 8 should be "if Authorization then" RobloxianDestory 262 — 7y

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

Let's "unroll" the for loop. For loops just do something once per thing in the container. So your code is currently equivalent to

do -- iteration 1
    local v = "NiniBlackJackQc"
    if player.Name == v or player.UserId == game.CreatorId then
        script.Parent:Destroy()
        GameSound()
        print("Administrator: "..player.Name, "Administrator Id: "..player.UserId)
        return; -- This stops the loop from running anymore.
    else
        Character:Destroy()
        Instance.new("BlurEffect",workspace.CurrentCamera)
        IntroSound()
        print("Player: "..player.Name, "Player Id: "..player.UserId)
        return;
    end
end

do -- iteration 2
    local v = "DAHUI84"
    if player.Name == v or player.UserId == game.CreatorId then
        script.Parent:Destroy()
        GameSound()
        print("Administrator: "..player.Name, "Administrator Id: "..player.UserId)
        return; -- This stops the loop from running anymore.
    else
        Character:Destroy()
        Instance.new("BlurEffect",workspace.CurrentCamera)
        IntroSound()
        print("Player: "..player.Name, "Player Id: "..player.UserId)
        return;
    end
end

Notice that you're never going to get passed the first loop: you unconditionally return having made a decision based on only the first name in the list.

Here is the code you meant to write. We can define isAuthorized later:

local Authorization = {"NiniBlackJackQc","DAHUI84"}

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character

local Character = player.Character

if isAuthorized() then
    script.Parent:Destroy()
    GameSound()
    print("Administrator: "..player.Name, "Administrator Id: "..player.UserId)
else
    Character:Destroy()
    Instance.new("BlurEffect",workspace.CurrentCamera)
    IntroSound()
    print("Player: "..player.Name, "Player Id: "..player.UserId)
end

This clearly doesn't have logic bugs, because it says to do exactly what you want (notice that since there's no more code there's no need for returns either)

isAuthorized is simple:

function isAuthorized()
    if player.UserId == game.CreatorId then
        return true
    end

    for _, name in pairs(Authorization) do
        if name == player.Name then
            return true
        end -- we can't say "else" because there are more names
    end
end
0
It works! Thank for your explanation and Merry Christmas! NiniBlackJackQc 1562 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago

I'm not an expert at scripting but I would try to change player to players because it is more than 1 player or I will try to make 2 separate script and make it with 1 of the names each instead of 2 times in one script. Hope I helped :) Merry Christmas!

0
Merry Christmas you too! NiniBlackJackQc 1562 — 7y
1
This would work but it's not a real solution. If you want to make a change to the script, you have to modify one copy per player authorized. It gets out of control real quick. BlueTaslem 18071 — 7y
0
I'm sorry, I am still learning LUA Merry Christmas! Goldenkings11 -11 — 7y

Answer this question