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

How do I fix this Join message for admins?

Asked by 8 years ago
Edited 8 years ago
01Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}
02 
03function PlayerJoined(player)
04 for i,v in pairs {Admins} then
05 if v == player
06 m = Instance.new("Message", game.Workspace)
07m.Text = "The Admin"..player.."has joined the server"
08 wait(5)
09 m:Destroy()
10 break
11  end
12  end
13end
14 
15game.Players:PlayerAdded:connect(PlayerJoined)

I hope I can fix this before funk's bday along with building tools before his bday!

3 answers

Log in to vote
2
Answered by 8 years ago

Your code has a few problems that would break your code and a few that are just good practice things.

First, in generic for loops, the syntax is for i, v in pairs(myTable) do. You used then instead of do.

Second, to call the pairs (or ipairs) function, you need parenthesis, () not curly braces {}

Third, every if requires a then, so your line 5 needs a then to complete the statement.

Fourth, and this is a little harder to catch, on your line 5, you compared the player object itself to the string you gave in the table. Comparing an object to a string always returns false. The way to fix this is to compare the .Name property of the player instead of the player itself.

01local Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}
02 
03function PlayerJoined(player)
04    for i,v in pairs(Admins) do
05        if v == player.Name then
06            m = Instance.new("Message", game.Workspace)
07            m.Text = "The Admin " .. player .. " has joined the server"
08            wait(5)
09            m:Destroy()
10            break
11        end
12    end
13end
14 
15game.Players:PlayerAdded:connect(PlayerJoined)

Now for the minor problems:

First, indentation is something that helps keep track of variable scope. In general, everything in a function is indented a level, everything in a for loop is indented a level, everything in an if statement is indented a level, and so on with while loops, repeat loops, etc.

Second, Messages are deprecated by ROBLOX, meaning they are no longer supported and are not guaranteed to work any longer.

Third, variables are normally created as local as local variables take less time to access.

Ad
Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Your code has a couple few syntax errors in it. For example, an if statement is formated as so: if condition then.

Even after you clean up all the syntax errors, you'll still find you aren't getting the correct result due to you if statement condition. You are asking: "if the player who joined is equal to this string then". What you want to be asking is: "if the player who joined's name is equal to this string then".

01local admins = {"Name", "AnotherName", "TheLastName"}
02 
03local function DisplayMessage(message, duration)
04    local message = Instance.new("Message", game.Workspace)
05    message.Text = message
06    wait(duration)
07    message:Destroy()
08end
09 
10local function PlayerAdded(player)
11    for _, name in ipairs(admins) do
12        if player.Name == name then
13            DisplayMessage("The Admin " .. name .. " has joined the server!", 5)
14            return
15        end
16    end
17end
18 
19game.Players.PlayerAdded:connect(PlayerAdded)
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

You got a ton of stuff wrong here.

First off, the first "if" you have there, doesn't have a then. All if statements must have a "then".

1if [condition] then [code] end

again with the if statement. In the condition, you have:

1v == player

you are checking if a string is the same as a player.

You need to check if a string is the same as a player name.

1if v == player.Name then

in the for statement, you have a then at the end of the first line of it.

1for i,v in pairs {Admins} **then**

replace the then with a do

1for i,v in pairs {Admins} do

But that's still incorrect. try calling pairs with parenthesis! ()

1for i,v in pairs(Admins) do

there we go! Also, try and connect the PlayerJoined function with the player.

1game.Players.PlayerAdded:connect(PlayerJoined(player))

And we did it! Technically this is fixed. But according to us, it's incorrect!

Whenever an admin joins the game, multiple messages overlap each other!

Why? Because it checks for admins every time it runs!

01Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}
02 
03function PlayerJoined(player)
04    local isAdmin = false -- notice my use of local variables
05    for _,v in pairs(Admins) do
06        if v:lower() == player.Name:lower() then
07            isAdmin = true
08            break
09        end
10    end
11    if (isAdmin == true) then -- you don't need parenthesis around the condition. It just looks clean.
12        local m = Instance.new("Message", game.Workspace) -- notice my use of local variables
13        m.Text = "The admin " .. player.Name .. " has joined the server."
14        wait(5)
15        m.Parent = nil -- m.Parent = nil is the same as m:Destroy()
16    end
17end
18 
19game.Players.PlayerAdded:connect(PlayerJoined(player))

Alright. What did I do? Well. Look at the for loop. I put an underscore there because we don't need the "i" variable. It is completely optional to remove the "i" variable but it makes the script cleaner.

I also did v:lower() and player.Name:lower(). lower() is a function to find the lowercase version of a string.

Example:

1Example = "UPPERCASE"
2print(Example:lower())

Output: "uppercase"

I also used, 'break'. Break breaks any loop.

Example:

1while true do
2print("HI")
3break
4end

output: HI

Do you know what local variables are? Local variables are local. (duh) Meaning they are only accessible by the function, loop, if, etc it is in.

Example:

1if true then
2local x = 1
3print(x)
4end
5print(x)

output: 1, nil

I hoped this helped.

If you're new to lua, I recommend you read programming in lua.

Here is a free version online: Programming in lua

And also, if you feel stupid reading my answer. Don't feel that way. Programming isn't something you learn instantly. It takes time to figure it out. I remember when I didn't know how to connect a function to an event. Lol.

0
When connecting events, you don't call the function and supply arguments, you just give the function name, such as: script.Parent.Touched:connect(onTouched) GoldenPhysics 474 — 8y
0
wiki.roblox.com/index.php?title=RBXScriptSignal#Usage GoldenPhysics 474 — 8y

Answer this question