I’m trying to detect what a player is saying to give certain commands to an NPC by checking if the message they send is equal to a certain prompt for said command, however, I’m finding that only one command is being received regardless of what you say, even if it’s not a string listed:
local player = game.Players.LocalPlayer
local event = game:GetService("ReplicatedStorage").Events.Coordinate
Enabled = false
player.Chatted:Connect(function(msg)
if Enabled == true then return end
Enabled = true
if msg:lower() == "stop" then
print(msg)
event:FireServer("Stop")
elseif msg:lower() == "grab" then
print(msg)
event:FireServer("Grab")
elseif msg:lower() == "follow" then
print(msg)
event:FireServer("Follow")
else
return
end
wait(0.5)
Enabled = false
end)
In a seperate script, I print the specific command which was sent through with the event:
Any ideas on whats going wrong here? I’m completely lost considering it should only be passing the event if the specific message is received.
I’ve played around with the script a little more, and it’s not the specific commands alone, rather if I list a lot of words which may trigger the event, it messes up. This is what I had in the script:
player.Chatted:Connect(function(msg)
if Enabled == true then return end
Enabled = true
if msg:lower() == "stop" or "stand still" or "stop now" or "wait" or "wait here" or "wait there" or "stop right there" or "stop there" or "stay" or "stay there" or "stay right there" or "freeze" or "stay put" or "halt" then
print(msg)
event:FireServer("Stop")
elseif msg:lower() == "grab" or "grab him" or "grab her" or "grab them" or "grab it" then
print(msg)
event:FireServer("Grab")
elseif msg:lower() == "follow" or "follow me" or "come on" or "come" or "come with me" then
print(msg)
event:FireServer("Follow")
else
return
end
wait(0.5)
Enabled = false
end)
I’m still unsure why it doesn’t allow me to check multiple strings, but if I just have a standalone word then it works perfectly fine.
Well, the main issue here is that if there is no possible command found, the script returns before able to set Enabled to false again, causing it to not be useable afterwards.
if RS.Events.Coordinate.OnServerEvent:Connect(function(player,action)
if Enabled == true then return end
Enabled = true
print(action)
Coordinating = true
spawn(function()
if action == "Stop" then
Commanded = true
configs.StandStill(npc)
configs.Grab(npc,action)
elseif action == "Follow" then
Commanded = true
orientNPC(npc,player)
while wait(updateTime) and action == "Follow" do
if targetX < configs.ActionDistance*2 then
orientNPC(npc,player.Character)
configs.StandStill(npc)
elseif targetX < configs.AggroDistance/10 then
configs.Aggro(player.Character,false)
end
end
end
end)
wait(0.4)
Enabled = false
wait(0.7)
Commanded = false
end) then
end
end
end
player.Chatted:Connect(function(msg)
if Enabled == true then return end
Enabled = true
if msg:lower() == "stop" or msg:lower() == "stand still" or msg:lower() == "stop now" or msg:lower() == "wait" or msg:lower() == "wait here" or msg:lower() == "wait there" or msg:lower() == "stop right there" or msg:lower() == "stop there" or msg:lower() == "stay" or msg:lower() == "stay there" or msg:lower() == "stay right there" or msg:lower() == "freeze" or msg:lower() == "stay put" or msg:lower() == "halt" then
print(msg)
event:FireServer("Stop")
elseif msg:lower() == "grab" or msg:lower() == "grab him" or msg:lower() == "grab her" or msg:lower() == "grab them" or msg:lower() == "grab it" then
print(msg)
event:FireServer("Grab")
elseif msg:lower() == "follow" or msg:lower() == "follow me" or msg:lower() == "come on" or msg:lower() == "come" or msg:lower() == "come with me" then
print(msg)
event:FireServer("Follow")
else
return
end
wait(0.5)
Enabled = false
end)
This worked, thank you.
Do you know if there’s any way I could condense this down into a table which can be checked… I feel it’ll get too bulky if I add these long lines like this everywhere
will always evaluate to either true or “stand still” (which isn’t nil or false) because that’s basically a lua ternary operator, therefore it will always run what’s in the first if condition
You have to compare each string separately with its own == like so
if msg:lower() == "stop" or msg:lower() == "stand still"
If you want, you can just make a table of words and then use table.find or if table[msg] == true