I need help with my food system

I’m still learning I made this script and I need help from you guys to make it work succesfully and better it works super rarely this is the script…

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)

The tools are in serverstorage.

What is the script supposed to do, and, what does it not do that you need help with?

1 Like

Basically the NPC greets you if you walk to him and if you tell a food like “Water” it will give it to you but it rarely gives you the tool it’s supposed to give

1 Like

and by rarely i mean it’s not built in it just doesnt work sometimes

– the function to check
if (string.sub(string.lower(item.Name) , 1 , string.len(partialName) ) ) == string.lower(partialName) then
print("Found Item: "…Item.Name)
return Item
end

so what this does is return the item for eg if the name is like water . "watter , wat , wa " but there is a problem in which if you have multiple items with the same starting character it will choose the the first one in the table.

there’s multiplie tools (extra letters))

I like this game element, sounds good. I presume you’ve added some debugging prints, to see what’s going on?

function extractToolFromMessage(msg)
	local words = string.split(msg, " ")
	print("Words found:",words) --TODO: Temp. Debugging.
	-- Loop through all words in the message to find the tool name
	for _, word in pairs(words) do
		local foundTool = findToolByPartialName(word)
      	      	print("Trying word: "..word,"Result:",foundTool) --TODO: Temp. Debugging.
		if foundTool then
			return foundTool
		end
	end
	return nil
end

Add those print statements, and look at the results carefully to see if the sentance is being parsed in the way you expect. This also confirms if extractToolFromMessage is being called. Then let us know… is ‘extractToolFromMessage’ being called as expected, and is it trying each word as expected, until it finds a match?

If everything is okay at this stage, next do similar debugging inside findToolByPartialName.