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

Check if Moderator or not will not fire?

Asked by 9 years ago

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)
0
Use CharacterAdded instead of plr:WaitForChild. EzraNehemiah_TF2 3552 — 9y

3 answers

Log in to vote
0
Answered by 9 years ago

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
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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.

0
He wants it there so that it prints when the player or mod's character enters the game rather than their player. Also, I think you meant to say he should put Moderators in ServerStorage rather than Copy. aquathorn321 858 — 9y
Log in to vote
0
Answered by 9 years ago

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

Answer this question