Hi guys, I want to make a commands script, so when you chat something, you get the appropriate result.
I is really time consuming if I check weather the message chatted by player is the command or not, writing 3 lines of code for each command! D:
How do I write it more efficiently? I though of using a table which contains commands and the relative subjects.
Here's my table. It changes faces
local Faces = { Smile = "http://www.roblox.com/asset/?id=7137738", AngryMin = "http://www.roblox.com/asset/?id=7079958", AngryMid = "http://www.roblox.com/asset/?id=7137750", AngryMax = "http://www.roblox.com/asset/?id=7604619", Kind = "http://www.roblox.com/asset/?id=7604617", HappyMin = "http://www.roblox.com/asset/?id=7079953", HappyMax = "http://www.roblox.com/asset/?id=7604621" }
and then, Here's my script... | It changes player's face.
local function changeFace(face) -- local because upon this there is a player added function. player.Character.Head.face.Texture = face end
player.Chatted:connect(function(message) for i = 1, #Faces do if message == #Faces[i] then changeFace(#Faces[i]) end end end) end)
How do I fix this thing? I am new to tables and for loops, So please help me.
Thankyou.
I think you're doing it just a little bit wrong.
I'm not sure if your table format is wrong, but I don't recognize it, so I'm just going to rewrite it as a dictionary.
local Faces = { "Smile" =["http://www.roblox.com/asset/?id=7137738"], "AngryMin" = ["http://www.roblox.com/asset/?id=7079958"], "AngryMid" = ["http://www.roblox.com/asset/?id=7137750"], "AngryMax" =[ "http://www.roblox.com/asset/?id=7604619"], "Kind" = ["http://www.roblox.com/asset/?id=7604617"], "HappyMin" = ["http://www.roblox.com/asset/?id=7079953"], "HappyMax" = ["http://www.roblox.com/asset/?id=7604621"] } ~~~~~~~~~~~~~~~~~\ Notice the differences between your table and my dictionary. Your table has variables with a string as a value, mine have variables named with a string, with string values. It's a little complicated to understand, but the Chatted() event doesn't work with your table unless you use `tostring(variable)`. Since the variables in your table aren't named with strings, you can't use the Chatted() event with them without `tostring(variable)`. Now to edit your for loop.
player.Chatted:connect(function(message) for faceName, faceId in pairs(Faces) do --this is how you use for loops with a dictionary if message == faceName then --checking if the message matches the name of a face in the table changeFace(faceId) --changes the face end end end) ~~~~~~~~~~~~~~~~~
I just modified your for loop to work with the dictionary. Basically, your only problem that I saw was that you tried to compare string with a userdata value. Just try my code and see if it works. Thanks!
If this answer is helpful, please upvote and accept!