Error using elseif and if

Well, I have problems identifying the name of a tool and depending on the value, a label (Eat, Drink, Increment_Health) is returned. I’ve already checked everything and it seems to be fine, from my point of view, it shouldn’t give the error. The error I have is that only tool.Name == "Agua" and "Hamburguesa" or "HotDog" or "Pan" or "Pastelillo" or "Sandwich" "works. then , the last elseif is the one that does not serve me, and I do not know the reason why.

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local toolsFolder = ServerStorage:WaitForChild("ToolsFolder")
local HungerAndThirstIncrease = ServerStorage:WaitForChild("HungerAndThirstIncrease")

local ProfileService = require(ServerScriptService:WaitForChild("DataStore"):WaitForChild("Configurations"):WaitForChild("DataManager"))

local stack = script.Parent:WaitForChild("Stack")

local anims = {script.Parent.Hit}
local loadedAnims = {}
local tool = script.Parent
local animator
local debounce = false
local del = 0.6
local function WhatTool()
	if tool.Name == "Agua" then
		print("3")
		return "Drink"
	elseif tool.Name == "Hamburguesa" or "HotDog" or "Pan" or "Pastelillo" or "Sandwich" then
		print("2")
		return "Eat"
	elseif tool.Name == "Vendajes" or "Pildoras" or "Mediquin"then
		print("1")
		return "Increment_Health"
	end
end

You have to check each condition individually:

elseif tool.Name == 'Hamburguesa' or tool.Name == 'HotDog' or tool.Name == 'Pan' ... then
1 Like
local function WhatTool()
	if tool.Name == "Agua" then
		print("3")
		return "Drink"
	elseif table.find({"Hamburguesa", "HotDog", "Pan", "Pastelillo", "Sandwich"}, tool.Name) then
		print("2")
		return "Eat"
	elseif table.find({"Vendajes", "Pildoras", "Mediquin"}, tool.Name) then
		print("1")
		return "Increment_Health"
	end
end

I fixed the function
basically it sees if the tool name is any of the names inside the table using table.find

1 Like
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local toolsFolder = ServerStorage:WaitForChild("ToolsFolder")
local HungerAndThirstIncrease = ServerStorage:WaitForChild("HungerAndThirstIncrease")

local ProfileService = require(ServerScriptService:WaitForChild("DataStore"):WaitForChild("Configurations"):WaitForChild("DataManager"))

local stack = script.Parent:WaitForChild("Stack")

local anims = {script.Parent.Hit}
local loadedAnims = {}
local tool = script.Parent
local animator
local debounce = false
local del = 0.6
local function WhatTool()
	if tool.Name == "Agua" then
		print("3")
		return "Drink"
	elseif tool.Name == "Hamburguesa" or tool.Name == "HotDog" or tool.Name == "Pan" or tool.Name == "Pastelillo" or tool.Name == "Sandwich" then
		print("2")
		return "Eat"
	elseif tool.Name == "Vendajes" or tool.Name == "Pildoras" or tool.Name == "Mediquin"then
		print("1")
		return "Increment_Health"
	end
end

As the first reply stated, if the conditional statement is composed of multiple parts through the use of Boolean logic operators (not, and, or etc.) you must specify the value you are comparing with in this case that value would be (tool.Name).