So basically, I am creating an admin command that uses string.find() to find the name of an enemy without me typing it’s full name.
AdminCommands.spawnenemy = function(player, originalMessage)
local splitMessage = string.lower(originalMessage)
originalMessage = splitMessage:split("#spawnenemy")
local enemyFinding = originalMessage[2]
if enemyFinding then
local trueenemyFinding = string.lower(enemyFinding)
local AllEnemies = game.ServerStorage:WaitForChild("Enemies"):GetChildren()
for index, enemy in pairs(AllEnemies) do
local trueEnemyName = string.lower(enemy.Name)
if string.find(trueEnemyName, trueenemyFinding) then
continue
else
table.remove(AllEnemies, index)
end
end
local foundEnemy = AllEnemies[1]
local clonedEnemy = foundEnemy:Clone()
clonedEnemy.Name = player.Name.."'s "..foundEnemy.Name
game.ReplicatedStorage.EnemyEvents:WaitForChild("SpawnEnemyAdmin"):Fire(clonedEnemy)
end
end
However, it fails to detect the name of the enemy.
I am using “holo” (trueenemyFinding) to try and detect some enemies.
I have many enemies’ names beginning with “holo”, So why does it not detect it?
For more info this is what I type in chat: “#spawnenemy holo”
And one enemy is named “Holographic Enemy”
This actually isn’t completely true. Both iterators can work interchangeably, with the exception of ipairs skipping over any keys. GetChildren() returns index-value pairs though, so it works fine regardless.
use stringmatch() stringstring.match ( string s, string pattern, number init = 1 )
Looks for the first match of pattern in the string s. If a match is found, it is returned; otherwise, it returns nil. A third, optional numerical argument, init, specifies where to start the search; its default value is 1 and can be negative.
I’ve tried both solutions and neither seemed to work.
I even tried printing out the name and the term I am using to find the enemy.
It comes out like this: “holographic enemy holo”
“Holographic Enemy” is the name and “holo” is the term I am using. It is clearly obvious that “holo” is in the name. All three (string.match(), string.find(), :match()) fail to work.
I also tried doing a separate line of code to test if something was broken.
This is what it said
if string.match("holographic enemy", "holo") then
print("agreed")
end
This is literally the same when it comes to be Holographic Enemy’s turn in the loop.
But for some reason, this line of code prints “agreed” meaning that it found a match. When I try to do the same in the loop, it always comes out false.
UPDATE: I have now fixed it. Apparently, the term I was using (holo) had a space in front of it to make it look like " holo". Just because of that singular space it made it unable to detect matching enemy names. So I just needed to use :split(" ") and basically it fixed it.