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

How can I make a conditional for this specific onChatted function?

Asked by 6 years ago

I want to make it so the player will get a badge if they say certain phrases but I barely have a clue as to how I would approach, In this script i've made it so the player receives a badge from saying one of the phrases, but I'm not sure how to make the script function with multiple phrases. Please help!

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeID = 00000000

function onChatted(msg, recipient, speaker) 

local source = string.lower(speaker.Name) 
msg = string.lower(msg) 

local player = game.Players:GetChildren()
    for i = 1, #player do
        if (msg == "phrase") then 
        BadgeService:AwardBadge(player[i].userId, badgeID)
    end
end
end

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 

i.e, if a player says "hello", "hi" and "hey" at any point in the server, they would receive a badge

0
you shouldn't use PlayerAdded and GetPlayers User#19524 175 — 6y
0
you should * User#19524 175 — 6y
0
I edited my answer to better help you out. User#21908 42 — 6y
0
Sorry I forgot that I should set it to zero first because I am adding to it. User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

There are a few problems with your code that I will attempt to address while also answering your question. The problem you have is relatively common. How do we check to see if one of many options works? There is are a few methods that I can think of. However, the one that I recommend for large amounts of options is thus: Use a table that contains all of the options.

local phrases = {"hello", "hey", "hi"} -- using the examples you provided

Now if we were to check the table using an if statement it would not work. Here is an example:

if msg == phrases then 

Why would this not work? The reason is that when you check if msg = phrases you are merely checking if the msg = table not if msg matches any of the actual items in the table. So how can we check against each item in the table? We can use a for loop similar to the one you used. Now we could do:

for i = 1, #phrases do -- here we are going to loop the length of the table
    if msg == phrases[i] then
        -- award badge
    end
end

But Lua has a useful way to loop through tables that is even easier than that. Here is how I would use it:

for i,v in pairs(phrases) do -- i is the index you are at and v is the value. You can name them whatever you want I just prefer i,v. pairs is the built in Lua function that allows you to loop through a table.
    if msg == v then
        -- award badge
    end
end

Now here is where I want to address some problems in your code. GetService() is the recommended method for getting services like the "Players" service. Here is how you use it:

local players = game:GetService("Players")
players.PlayerAdded:Connect(function(plr) -- PlayerAdded event is a more secure event than ChildAdded is because if something other than a player enters Players the code will not error I also like to use a anonymous function so I don't have to prewrite one.
    plr.Chatted:Connect(function(msg) -- another anonymous function
        for i,v in pairs(phrases) do -- you could have this as a function that you call if you want to
            if msg == v then
                -- award them the badge
            end
        end
    end)
end)

The reason I got rid of the loop you had on line 12 is because that would affect all the players. Every player on the server would have gotten a badge if one player did the action necessary to get the badge. Also you should make sure the player does not already have the badge before awarding it to them. :connect() is deprecated you should use :Connect() instead. The variable source seems unnecessary to me but you might have another use for it that I do not know. I hope that this answer helps and that you will have a great day scripting! Edit: If you wanted them to have to say all of the words at once then you would have to do something like this:

players.PlayerAdded:Connect(function(plr) 

    local amountOfWordsSaid = 0

    plr.Chatted:Connect(function(msg) 
        for i,v in pairs(phrases) do 
            if string.match(msg, v) then then -- if the msg contains the item in the list phrases then
                amountOfWordsSaid = amountOfWordsSaid +1 -- the amount of correct words they have said in their message goes up by one
                if amountOfWordsSaid == #phrases then -- if they have said all the words in the list phrases then
                -- award them the badge
            end
        end
    end)
end)

Hope this better fits your question!

0
THANK YOU! beautifully explained and extremely helpful. However, I still have no clue how to make it so that if the player says ALL the phrases then they'll receive the badge, your script still gives the badge when the player says one of the phrases, unless im missing something Terpsichora 4 — 6y
0
thank you again, honestly a life saver, I just set the variable amountOfWordsSaid = 0, i believe that should work? Terpsichora 4 — 6y
0
oh yeah User#21908 42 — 6y
Ad

Answer this question