✓ Accepted answer
##Answer
It doesn't.
##Problem
- 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.
- You have the function onChatted which is not called anywhere else in the script.
- You also have two arguments that are never involved in the function as well.
- Fourth, you're missing an end for the second if then statement.
##Solution
- 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.
- Within the chatted event function, call the function that handles the command.
- Within the function call you're using, have the variables for the player and their message in the parenthesis.
- 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
player.TeamColor = BrickColor.new("Eggplant")
player:LoadCharacter()
end
end
end
function PlayerAdded(player)
player.Chatted:connect(function(Chat)
onChatted(Chat, player)
end)
end
game.Players.PlayerAdded:connect(PlayerAdded)
for _,player in pairs(game.Players:GetPlayers()) do
PlayerAdded(player)
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.
7 comments
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
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.
I posted the full script below
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.
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.
You just needed to say that! Thank you, I get it now
"--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...