Scripting error

Basically.

It will greet you if you walk to him this one works.

But if you say the tool name ex: Water it doesn’t give it somewhy and I can’t find the issue.

I also have alot of sorts water so it just gives one sort everytime but I have like 5 sorts.

local Head = script.Parent
local DetectPart = script.Parent:WaitForChild("DetectedPart")
local ServerStorage = game:GetService("ServerStorage")
local ChatService = game:GetService("Chat")

local TouchedPlayers = {}
local Cooldowns = {}

DetectPart.Touched:Connect(function(hit)
	local player = game.Players:FindFirstChild(hit.Parent.Name)
	if player then
		local playerName = player.Name
		if not table.find(TouchedPlayers, playerName) and (not Cooldowns[playerName] or os.time() - Cooldowns[playerName] >= 5) then
			ChatService:Chat(Head, "Welcome "..playerName.."! What would you like today?")
			table.insert(TouchedPlayers, playerName)
			Cooldowns[playerName] = os.time()
		end
	end
end)

DetectPart.TouchEnded:Connect(function(hit)
	local player = game.Players:FindFirstChild(hit.Parent.Name)
	if player then
		local playerName = player.Name
		local index = table.find(TouchedPlayers, playerName)
		if index then
			table.remove(TouchedPlayers, index)
		end
	end
end)

-- Function to find a tool by partial name
function findToolByPartialName(partialName)
	for _, item in pairs(ServerStorage:GetChildren()) do
		if string.match(string.lower(item.Name), string.lower(partialName)) then
			return item
		end
	end
	return nil
end

-- Function to extract tool name from the player's message
function extractToolFromMessage(msg)
	local words = string.split(msg, " ")
	-- Loop through all words in the message to find the tool name
	for _, word in pairs(words) do
		local foundTool = findToolByPartialName(word)
		if foundTool then
			return foundTool
		end
	end
	return nil
end

local greetings = {"hi", "hello", "sup", "hey", "yo", "greetings", "what's up"}
local greetingsai = {"Hello!", "Hi!", "Sup!", "Hey there!", "Greetings!", "What's up?"}

function isGreeting(msg)
	local lowerMsg = msg:lower()
	for _, greeting in pairs(greetings) do
		if lowerMsg == greeting or lowerMsg:match("^" .. greeting) then
			return true
		end
	end
	return false
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Msg)
		if table.find(TouchedPlayers, Player.Name) then

			if isGreeting(Msg) then

				local function getRandomGreeting()
					return greetingsai[math.random(1, #greetingsai)]
				end

				ChatService:Chat(Head, getRandomGreeting())
				
				return
			end

			local foundTool = extractToolFromMessage(Msg)

			if foundTool then
				if foundTool.Name == "Economy" or foundTool.Name == "Frequent Flyer" or foundTool.Name == "Galactic VIP" or foundTool.Name == "Drinks and Snacks Menu" then
					ChatService:Chat(Head, "Sorry, I couldn't find the item you're looking for. Please make sure to enter the name exactly as it appears in the menu.")
				else
					foundTool:Clone().Parent = Player.Backpack
				end
			else
				ChatService:Chat(Head, "Sorry, I couldn't find the item you're looking for. Please make sure to enter the name exactly as it appears in the menu.")
			end
		end
	end)
end)

It sometimes gives me something but not always.

All the tools are inside of Serverstorage.

print this variable out. What does it give you? Nil? An instance? If it’s an instance, check its name.

But if it’s nil… print out the “word” variable (itll print multiple words since it’s in a for loop).

.

To just:

return ServerStorage:FindFirstChild(partialName)

I don’t believe that he can, as your proposed solution checks if the whole word matches rather than the partial match found in the original code.

1 Like

@SeargentAUS was talking about the same loop. The code you quoted is responsible for allowing users to input partial tool names as opposed to exact tool names. Your proposed change eliminates this feature. Nonetheless, your proposal deserves consideration as the proceeding code prompts the user to “make sure they enter the name exactly as it appears”. Ultimately, OP’s code does work, so I think we’ll need some more information from them before continuing

1 Like

My bad, misinterpreted that line of code. When I read it, I forgot how string.match worked lol

I find it very difficult to understand what your problem is. Is your script raising an error? is it not performing the way you expect it to? If it’s an error, please provide it to us. If it’s a performance concern, please elaborate on what your expected outcome is Vs. the actual outcome