Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Authorization script, it not works for both name why?

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

1 comment

0

i think line 8 should be "if Authorization then"

2 answers

✓ Accepted answer

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

1 comment

0

It works! Thank for your explanation and Merry Christmas!

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!

3 comments

0

Merry Christmas you too!

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.

0

I'm sorry, I am still learning LUA Merry Christmas!

This discussion is preserved as it was. New answers, comments, and votes are closed.