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

What's happening with my check player name script?

Asked by 9 years ago

My script will take a BillboardGUI from ReplicatedStrorage and clone it to the players head, assuming they're a Mod. If they are not, not it will print that a player has entered the game. Here's the Script inside of Workspace (Not a Local Script)

local Mod = {"LegendaryArmor","alphawolvess","Player"}
local GameName = "FlobbyGames"

game.Players.PlayerAdded:connect(function(plr)
    plr:WaitForChild("Character")
    for i,Mods in pairs(Mod) do
        if plr.Name == Mods then
            print("A moderator has entered "..GameName.."!")
            local Copy = game.ReplicatedStorage.Moderators:Clone()
            Copy.Parent = plr.Character.Head
        elseif plr.Name ~= Mods then
            print("A player has entered "..GameName..".")
        end
    end
end)

There are no errors. I'm using "Player" because that's the Test character's name.

2 answers

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

Very first issue: Why are you using Mod for the list and Mods for the elements? That's opposite the reasonable convention.


Second issue: The testing player is called "Player1", not "Player".


Third: No reason to use if a then ... elseif not a then. Just use else.

However, in this case, this is wrong. You are checking if the first mod matches, and if it's not the first one, then do the other thing.

Instead, we'll have to keep track of whether or not they are a mod:

local isMod = false -- We have not yet found their name in the list
for _, mod in pairs(mods) do
    if mod:lower() == plr.Name:lower() then
        isMod = true -- We have found their name in the list!
    end
end

if isMod then
    print("A moderator has entered " .. GameName .. "!")
else
    print("A player has entered " .. GameName .. "!")
end
0
Um, when I press play in studio, I see no where saying Player1? All I see is Player. I can take a photo and put on a website to show? alphawolvess 1784 — 9y
0
It's either Player1 or Player. Roblox switches names interchangeably; it's weird. Redbullusa 1580 — 9y
0
I said Mod because it would currently look at 1 mod, then the other... for the Table, it has more than 1. Just wondering, but is putting the exact name inside of a Table work as well? (Ik what :lower() does) alphawolvess 1784 — 9y
0
Oh, I did type Mod and Mods in the wrong spot. Sorry, sometimes my mind skips some things. alphawolvess 1784 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Flaws with your script:

-You can't use plr:WaitForChild("Character") because the character is not a child of the player. We can use a while loop or the CharacterAdded() event, though a while loop is better to use further down in the code, since if we use CharacterAdded(), it might not pick up on the character spawning the first time depending on how long it takes the previous code to run.

-The test name for the player has two interchangeable names: "Player" and "Player1". Not sure why, but you still have to compensate for it. We can fix this by putting both names in the table.

-You have the if statement inside of the for loop. That means that for each iteration through the table, if the mod is not a player, then it will print "A player has entered "..GameName.."!". We can fix this by using a variable that is true if the player is a mod, and use it to create an if statement that prints a mod has entered, otherwise a player has entered.

-Additionally, I'm assuming you want to clone the part into a moderator's character whenever he/she spawns instead of just once. For this, we can use the Character Added event.

I went ahead and made a script in super-safe mode, so you can use it with no risk.

Finished Script:

local Mods = {"LegendaryArmor","alphawolvess","Player","Player1","aquathorn321"}
local GameName = "FlobbyGames"

game.Players.PlayerAdded:connect(function(plr)
local b = false--define b as false locally, so that we may use it in PlayerAdded() without variable leaks.
local player = false--define player as false locally, so that we may use it in PlayerAdded() without variable leaks.
local m--define m locally, so that we may reference it in the CharacterAdded() event without a possibility of variable leaks.
m = plr.CharacterAdded:connect(function(character)--runs whenever a character is added
while not (b or player) do--loop until either variable is true
wait()
end--close while loop
if player then m:disconnect()--stop checking to see when the character is added if the player is not a mod
else--otherwise, run the following code
local Copy = game.ReplicatedStorage.Moderators:Clone()
Copy.Parent = character.Head
end--close if statement
end)--close CharacterAdded() event
for i,Mod in pairs(Mods) do
if plr.Name:lower() == Mod:lower() then
b = true
break--end the loop to stop it from running unnecessarily
end--close if statement
end--close for loop
while not plr.Character do--wait until character is found
wait()
end--close while loop
if b then
print("A moderator has entered "..GameName.."!")
else
print("A player has entered "..GameName.."!")
player = true
end--close if statement
end)--close PlayerAdded() event

Answer this question