Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can I check for my "whitelisted" names (line 1) and perform an action if a name is matched?

Asked by 6 months ago

Within my If statement, I'm trying to detect multiple player names locally.

local Players = 'NAME1', 'NAME2', 'NAME3' --etc.
local Player = game.Players.LocalPlayer

if Player.Name == Players  then 
    print(Player.Name)
elseif Player.Name ~= Players then 
    warn("Name did not match whitelisted players!")
end

I get my custom "Error" message (warn message) because it did not detect the player names. I am trying to check for the names it's supposed to check for and perform a function after.

1 answer

Log in to vote
0
Answered by
Isaque232 171
6 months ago

Hello, you can make the Players variable a table and iterate through it checking if Player.Name is equal to one of the names at the Players table.

On this example I'll be using the for loop to iterate through the Players table.

local Players = {'Name1', 'Name2', 'Name3'} -- Made it a table by adding { }
local Player = game:GetService("Players").LocalPlayer
local FoundPlayer = false

for i = 1, #Players do -- Iterates through the ' Players ' table

    if Player.Name == Players[i] then -- Checks if the LocalPlayer's name is in the table.
        FoundPlayer = true -- If it is then FoundPlayer will be set to true.
        print(Player.Name)
    end
end

if FoundPlayer == true then -- If FoundPlayer == true ( If Player.Name is in the table ) do
    print(" Player is in table ") -- (... Your code)
else
    print(" Player is not in table ") -- (... Your code)
end

Hopefully this solves your issue! You can see more about loops such as the for loop I've used here: Roblox Coding Basics - Loops

0
Awesome, thank you! Works perfectly! vislbIe 6 — 6mo
Ad

Answer this question