Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if an item the mouse is hovering over is in a table?

Hi. I am trying to make it so that when a player hovers over an ore while the tool is equipped, then it will change the gui's text to whatever ore it was. The ore is a model, hence the Parent.Name.

local tool = script.Parent
local handle = tool.Handle
local equipped = false

local player = game.Players.LocalPlayer
local cursor = player:GetMouse()
local cursorPos = cursor.Target
local ore = {"BaseOreRock", "CopperOre", "IronOre", "SilverOre", "GoldOre", "EmeraldOre", "RubyOre", "SapphireOre", "DiamondOre", "PlatinumOre"}
local gui = player:WaitForChild("PlayerGui"):WaitForChild("OreGui").TextLabel

tool.Equipped:Connect(function()
	cursor.Move:Connect(function(mouse)
		cursorPos = cursor.Target
		pcall(function()
			local cursorObject = cursorPos
			if cursorObject.Parent.Name == ore then
				gui.Text = cursorObject.Name
			else
				gui.Text = "None"
			end
		end)
		
	end)
	
end)

4 answers

✓ Accepted answer

Hello again, If you're trying to ask whether the Item is in a table, this is what you would do:

local tool = script.Parent
local handle = tool.Handle
local equipped = false

local player = game.Players.LocalPlayer
local cursor = player:GetMouse()
local cursorPos = cursor.Target

local ore = {"BaseOreRock", "CopperOre", "IronOre", "SilverOre", "GoldOre", "EmeraldOre", "RubyOre", "SapphireOre", "DiamondOre", "PlatinumOre"}

local gui = player:WaitForChild("PlayerGui"):WaitForChild("OreGui").TextLabel
tool.Equipped:Connect(function()
    cursor.Move:Connect(function(mouse)
        cursorPos = cursor.Target
        pcall(function()
            local cursorObject = cursorPos
                for _,ore in ipairs(ore) do --// runs through table 'ore', if the cursorPos' parent's name is equal to whatever index it's currenty running through, then let it pass
		   if cursorPos.Parent.Name == ore then
                      gui.Text = cursorObject.Name
                   end
                end
            else
                gui.Text = "None"
            end
        end)
    end)
end)

Nice to help you once again, don't forget to accept and upvote if it helped, I'll help you with anything else if you need!

10 comments

0

You beat me to it :(

0

I feel you:) I've worked with Fierce before, I had a quick answer to it, since I've been writing something very similar

0

Hiya. Thanks for the help! However, when I went and tested it out, it only worked for the last item in the table (PlatinumOre). The crazy thing is, when I do 'print(cursorObject.Parent.Name)' instead of gui.Text, it works perfectly fine. Hmm.

0

Let me take a look

0

This could be an issue with spelling? Check if they're exact

0

Just saw that it was 'ipairs' instead of 'pairs'. Changed that, but still had the same issue

0

I just posted what I had so far to the answers. Have a look

0

Nevermind, I found out the problem. Once I got rid of gui.Text = "None", the problem disappeared.. it was so weird. But I thank you for the help!

0

Hm, that's interesting, I've been having absurd issues like that too, If I make something nil, it freezes my mouse;)

0

Glad to help

tool.Equipped:Connect(function()
    cursor.Move:Connect(function(mouse)
        cursorPos = cursor.Target
        pcall(function()
            local cursorObject = cursorPos
            local FoundOre = false
            for _,iore in pairs(ore) do
                if cursorObject.Parent.Name == iore then
                    print(cursorObject.Parent.Name)
                    gui.Text = cursorObject.Parent.Name
                    FoundOre = true
                end
            end 
            if FoundOre == false then gui.Text = "None" end
        end)
    end)
end)

You cannot name the Value and the Table the same thing. If this helped, upvote/accept it!

1 comment

0

You did it! You fixed the issue! Thank you and simplealgorithm for helping me out. I really appreciate it! Thank you!!

ore is a table, so you would have to make a generic for loop for it.

function CheckIsOre(obj)
   for i, v in pairs(ore) do -- Loops through the table
      if obj.Name == v then -- Checks if the name is the same with the value
         return true -- Returns true
      end
   end
   return false -- If no match is found, return false
end

And to check it,

if CheckIsOre(cursorObject.Parent) then

Hope this helps and if it did, don't forget to accept :)

1 comment

0

Hi there. This works as well, but the same problem is occuring as stated in simplealgorithm's answer ^

This is what I have so far. Added for i,v but now it only outputs the last ore to the gui, rather than every other ore when the mouse is hovered. Every other ore works when printed though..

tool.Equipped:Connect(function()
	cursor.Move:Connect(function(mouse)
		cursorPos = cursor.Target
		pcall(function()
			local cursorObject = cursorPos
			for _,ore in ipairs(ore) do
				if cursorObject.Parent.Name == ore then
					print(cursorObject.Parent.Name)
					gui.Text = cursorObject.Parent.Name
				else
					gui.Text = "None"
				end
			end	
		end)
		
	end)
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.