I had help from Blue on another script. I put his together with mine and I believe I may have done something wrong? This script see's if someone is a mod, and if so their character will have a BillboardGui cloned from ReplicatedStorage to their head. I am not getting either of the prints?
Here's the Script (Not Local)
local Mods = {"LegendaryArmor","alphawolvess","Player"} local GameName = "FlobbyGames" local isMod = false game.Players.PlayerAdded:connect(function(plr) plr:WaitForChild("Character") for _, Mod in pairs(Mods) do if Mod:lower() == plr.Name:lower() then isMod = true end end if isMod then local Copy = game.ReplicatedStorage.Moderators:Clone() Copy.Parent = plr.Character.Head print("A moderator has entered "..GameName.."!") else print("A player has entered "..GameName.."!") end end)
Flaws with your script:
-Your first and foremost flaw: You defined "isMod" outside of the PlayerAdded() event. That means that it's changed whenever a mod enters the game, and every player after will be considered a mod whether they are or not.
-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
game.ReplicatedStorage
. That would imply that this script is a local script, of which you've indicated that it's not. What I would suggest is to parent Copy
to ServerStorage.
Also, on line 6, don't use the :WaitForChild()
to wait for the character. The character model will be an object of the player instance. If you left line 6 as is, it'll continually pause the script (thread) until an object by the name of "Character" is a parent of the player, rather than the property.
repeat wait() until plr.Character
Perhaps use this if you really wish to alter the player instance's character model.
Looking at your script, though, it seems that you don't need line 6 at all.
I'm going to give a relatively short, but hopefully well-explained answer. As others have said, on line 6, you're waiting for the player's character using plr:WaitForChild("Character")
. However, plr.Character
is simply a reference to another object; not an actual child of the player. Instead of doing this, it would be much better to do...
while not plr.Character do -- while plr.Character is nil wait() end
At this point, for redundancy, you may want to make sure that the player's head exists.
local character = plr.Character local head = character:WaitForChild("Head")
Finally, you may also want to consider setting the BillboardGUI's Adornee
property to the player's head, depending on the situation you're in.
One last thing: isMod
should definitely be defined in the PlayerAdded
event. Else, if a moderator joins, all others who join will also be selected as moderators, since isMod
will remain true
.
I hope this helps! - Programmix