String.find(arg1, arg2)?

I’m working on a wand system that utilises the Players text –

However, I’ve ran into an issue now where using string.find(arg1, arg2) isn’t returning accurate strings…

To elaborate, say I have 2 spells:

  • Spell1: Lumos.
  • Spell2: Lumos Maxima.

When the Player Chat’s Spell2, it’ll register Spell1 (Due to having ‘Lumos’ in the name.) – How can I avoid this?

I want it so the Spell can be seleted if the Player says it at any part of their chat as opposed to being direct. i.e.: “Lumos!” or “Lumos Maxima!” or “I wish to cast Lumos.” or “I wish to cast Lumos Maxima.”.
I’m quite the novice with String functions!

Thank you.

To make sure that Lumos doesn’t trigger lumos maxima, you can say.

if string.find(string, “Lumos”) and not string.find(string, “Maxima”) then

1 Like

Another issue with that is I use a table to store all of the names in & allow the Player to find it on chat.
i.e.

for __,Spell in pairs (Spells:GetChildren()) do
if string.find(PlayerText:lower(), Spell.Name:lower() then
-- Assign the spell.
end
end

add an if statement to check if the spell is Lumos, then do the statement I posted.

Reverse the order that they are tested in.
Instead of testing if they said Lumos and later testing if they said Lumos Maxima, first test if they said Lumos Maxima and then test if they said Lumos.
Once you’ve confirmed something was said, cast the related spell, then don’t check anything else. Just add return to the function to stop anything beyond it from happening.