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

Arguments and Parameters, I don't understand it?

Asked by 8 years ago
GroupId = 290188
MinimumRank = 25

function onChatted(msg,player)
    if player:GetRankInGroup(GroupId) >= MinimumRank then

if msg:sub(1,5) == "!Host" then
    player.TeamColor = BrickColor.new("Eggplant")
    player:LoadCharacter()

    end
end

How does the script know that the variable/argument "player" is the LocalPlayer?

2 answers

Log in to vote
4
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

Answer

It doesn't.


Problem

  1. Chatted scripts and scripts dealing with PlayerAdded of this nature should be handled in a Server Script. You can not access LocalPlayer through server side scripts normally as mentioned in the blog article Common Mistakes. Since you mentioned LocalPlayer, I thought I should clear that up.
  2. You have the function onChatted which is not called anywhere else in the script.
  3. You also have two arguments that are never involved in the function as well.
  4. Fourth, you're missing an end for the second if then statement.

Solution

  1. Use a server script and use the PlayerAdded event. Inside the function for the PlayerAdded event, you can connect a function to the Chatted event.
  2. Within the chatted event function, call the function that handles the command.
  3. Within the function call you're using, have the variables for the player and their message in the parenthesis.
  4. Add that third end necessary for the if-then statement to finish.

Recommendation

Since Roblox, at the time of this post, broke the PlayerAdded event in Play Solo mode. You will need to add a for loop at the bottom of the script to loop through all players and connect them to the PlayerAdded event. This is so you may test the script properly in play solo mode.

Also, when dealing with commands, make the chat and command the same case (as in upper or lower case). Otherwise, it might annoy the heck out of the user for not having the correct capitalization of a command.


Final Script

GroupId = 290188
MinimumRank = 25

function onChatted(msg,player)
    if player:GetRankInGroup(GroupId) >= MinimumRank then
        if msg:sub(1,5):lower() == "!host" then --Just add :lower() to the end of the string to make whatever they say lowercase to the script. This way commands are not case sensitive.
              player.TeamColor = BrickColor.new("Eggplant")
              player:LoadCharacter()
          end --That third end you forgot.
    end
end

function PlayerAdded(player) --Player is given to us by the PlayerAdded event. It's the physical player object that will be used.
    player.Chatted:connect(function(Chat) --.Chatted is an event of a player. Every time they chat, it goes through this event. This is an anonymous function. It's not exactly a function we can call, more of the function we are using. Chat is our string that the player has said.
        onChatted(Chat, player) --Now we are calling the function Chat being msg to the OnChatted function, and player being player to the function.
    end) --Annonymous functions have an end with a ) to show the end of the connect function.
end

game.Players.PlayerAdded:connect(PlayerAdded)
for _,player in pairs(game.Players:GetPlayers()) do --For every player in Players service, do something.
    PlayerAdded(player) --Alright, we'll add them to the function we created.
end

Hopefully, this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to post them in the comments below.
0
I already did that before, I just didn't post the whole script because it's really long due to other commands I put in. Unfortunatly this didn't answer my question, I don't mean "LocalPlayer" I meant like the Player under the "Players" folder. For example, if I changed my argument player to something like "random" it'd still work. How does the script do that? I haven't defined the argument (I read Willie3838 5 — 8y
0
Well, I am not fully understanding your question then. I would not know how the onChatted command gets a hold of the player or message through what you have shown me. M39a9am3R 3210 — 8y
0
I posted the full script below Willie3838 5 — 8y
0
Why not just edit your question? Plus, I basically went over it in my answer. The PlayerAdded event will provide the physical player that just joined the game, it was part of my recommendation to add a loop to go through all players to test in Play Solo properly. I also mentioned that the Chatted event is an event for player objects which will provide a string to your function. M39a9am3R 3210 — 8y
View all comments (3 more)
0
Whenever most events are fired, they will provide an object, string, boolean, or some type of value that the function can use as an argument. When you're calling your function on line 101, you're providing the player and message values the events on line 99 and 100 provide. M39a9am3R 3210 — 8y
0
You just needed to say that! Thank you, I get it now Willie3838 5 — 8y
0
"--Player is given to us by the PlayerAdded event. It's the physical player object that will be used.", "--.Chatted is an event of a player. Every time they chat, it goes through this event. This is an anonymous function. It's not exactly a function we can call, more of the function we are using. Chat is our string that the player has said." I did... M39a9am3R 3210 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
GroupId = 290188
MinimumRank = 25
----------------------------------------------------

------------------------------------------------------

function onChatted(msg,player)
    if player:GetRankInGroup(GroupId) >= MinimumRank then
    if msg:sub(1, 8):lower() == "!poweron" then
        game.Lighting.FogEnd = 1000000
        wait(2)
    game.Lighting.Brightness = 100
    wait(3)
    game.Workspace.Stairs.Stair.BrickColor = BrickColor.Yellow()
    wait(0.3)
    ------------------------------------------------------------------------
    game.Workspace.Stairs2.Stair.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs2.Model2.Stair2.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs2.Model2.Stair25.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs2.Model.Stair3.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs2.Model.Stair35.BrickColor = BrickColor.Yellow() 
    wait(0.3)
    -------------------------------------------------------------------------
    game.Workspace.Stairs5.Model2.Stair2.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs5.Model2.Stair25.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs5.Stair.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs5.Model.Stair3.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs5.Model.Stair35.BrickColor = BrickColor.Yellow()
    wait(0.3)
    ------------------------------------------------------------------------
    game.Workspace.Stairs3.Stair.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs3.Model2.Stair2.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs3.Model2.Stair25.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs3.Model.Stair3.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Stairs3.Model.Stair35.BrickColor = BrickColor.Yellow()
    wait(0.3)
    ------------------------------------------------------------------------
    -----------------------------END OF STAIRS------------------------------
    game.Workspace.Line.Line1.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Line.Line2.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Line.Line3.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.Line.Line4.BrickColor = BrickColor.Yellow()
    wait(0.3)
    game.Workspace.SideLines.Part1.BrickColor = BrickColor.Yellow()
    wait(0.3)
        game.Workspace.SideLines.Part2.BrickColor = BrickColor.Yellow()
        wait(0.3)
            game.Workspace.SideLines.Part3.BrickColor = BrickColor.Yellow()
            wait(0.3)
                game.Workspace.SideLines.Part4.BrickColor = BrickColor.Yellow()
                wait(0.3)
                    game.Workspace.SideLines.Part5.BrickColor = BrickColor.Yellow()
                    wait(0.3)
                        game.Workspace.SideLines.Part6.BrickColor = BrickColor.Yellow()
                        wait(0.3)
                            game.Workspace.SideLines.Part7.BrickColor = BrickColor.Yellow()
                            wait(0.3)
                                game.Workspace.SideLines.Part8.BrickColor = BrickColor.Yellow()
                                wait(0.3)
--------------------------------------END OF LINES------------------------------------------------
--------------------------------------END OF POWER ON---------------------------------------------


    end
    end 

    if msg:sub(1,5):lower() == "!host" then 
    player.TeamColor = BrickColor.new("Eggplant")
    player:LoadCharacter()
    game.Lighting.Host:Clone().Parent = player.PlayerGui
    wait(5)
    player.PlayerGui.Host:Remove()




    end
    end





game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) 
    onChatted(message, player) 
    end)
end)

Answer this question