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 7 years ago
Edited 7 years ago
Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}

function PlayerJoined(player)
 for i,v in pairs {Admins} then
 if v == player 
 m = Instance.new("Message", game.Workspace)
m.Text = "The Admin"..player.."has joined the server"
 wait(5)
 m:Destroy()
 break
  end
  end
end

game.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 7 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.

local Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}

function PlayerJoined(player)
    for i,v in pairs(Admins) do
        if v == player.Name then
            m = Instance.new("Message", game.Workspace)
            m.Text = "The Admin " .. player .. " has joined the server"
            wait(5)
            m:Destroy()
            break
        end
    end
end

game.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
7 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".

local admins = {"Name", "AnotherName", "TheLastName"}

local function DisplayMessage(message, duration)
    local message = Instance.new("Message", game.Workspace)
    message.Text = message
    wait(duration)
    message:Destroy()
end

local function PlayerAdded(player)
    for _, name in ipairs(admins) do
        if player.Name == name then
            DisplayMessage("The Admin " .. name .. " has joined the server!", 5)
            return
        end
    end
end

game.Players.PlayerAdded:connect(PlayerAdded)
Log in to vote
0
Answered by 7 years ago
Edited 7 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".

if [condition] then [code] end

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

v == 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.

if v == player.Name then

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

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

replace the then with a do

for i,v in pairs {Admins} do

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

for i,v in pairs(Admins) do

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

game.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!

Admins = {'Luistheinge', 'Funkvolume24', 'giganticCRAZYERIC'}

function PlayerJoined(player)
    local isAdmin = false -- notice my use of local variables
    for _,v in pairs(Admins) do
        if v:lower() == player.Name:lower() then
            isAdmin = true
            break
        end
    end
    if (isAdmin == true) then -- you don't need parenthesis around the condition. It just looks clean.
        local m = Instance.new("Message", game.Workspace) -- notice my use of local variables
        m.Text = "The admin " .. player.Name .. " has joined the server."
        wait(5)
        m.Parent = nil -- m.Parent = nil is the same as m:Destroy()
    end
end

game.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:

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

Output: "uppercase"

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

Example:

while true do
print("HI")
break
end

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:

if true then
local x = 1
print(x)
end
print(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 — 7y
0
wiki.roblox.com/index.php?title=RBXScriptSignal#Usage GoldenPhysics 474 — 7y

Answer this question