Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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!

0
You beat me to it :( SirDerpyHerp 262 — 5y
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 Ziffixture 6913 — 5y
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. FierceLeviathan 10 — 5y
0
Let me take a look Ziffixture 6913 — 5y
View all comments (6 more)
0
This could be an issue with spelling? Check if they're exact Ziffixture 6913 — 5y
0
Just saw that it was 'ipairs' instead of 'pairs'. Changed that, but still had the same issue FierceLeviathan 10 — 5y
0
I just posted what I had so far to the answers. Have a look FierceLeviathan 10 — 5y
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! FierceLeviathan 10 — 5y
0
Hm, that's interesting, I've been having absurd issues like that too, If I make something nil, it freezes my mouse;) Ziffixture 6913 — 5y
0
Glad to help Ziffixture 6913 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago
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!

0
You did it! You fixed the issue! Thank you and simplealgorithm for helping me out. I really appreciate it! Thank you!! FierceLeviathan 10 — 5y
Log in to vote
0
Answered by 5 years ago

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 :)

0
Hi there. This works as well, but the same problem is occuring as stated in simplealgorithm's answer ^ FierceLeviathan 10 — 5y
Log in to vote
0
Answered by 5 years ago

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)

Answer this question