I want to check my table from :GetChildren(). I want to see if the mouse's targeted brick or item is in the table I have.
I thought I could match the name of the target to the name in the table, but I don't know how to span my table like this. Would I just use a for loop?
player = game.Players.LocalPlayer mouse = player:GetMouse() Selected = script.Selected ConsumeEvent = game.ReplicatedStorage.Consume stat = script.Parent:WaitForChild("Stats") local consumables = game.Workspace.Consumables:GetChildren() -- table here mouse.Move:connect(function() if mouse.Target ~= nil then if mouse.Target.Name == consumables[WhatDoIPutHere].Name then -- main problem here script.Parent.BrickStatus.Main.Visible = true script.Parent.BrickStatus.Main.DisplayName.Text = mouse.Target.Name script.Parent.BrickStatus.Main.Portions.Text = mouse.Target.Portions.Value
This is just a portion of the script. There's obviously ends at the end of all this.
Hello.
Answer:
Yes, you should use an in pairs loop
. Use it by looping through the consumables and checking if the mouse's target's name is equal to the value's name.
Recommendations:
Use UserInputService's InputChanged event as Mouse.KeyDown is deprecated/outdated.
Use ServiceProvider:GetService() when indexing services.
Fixed Code:
local UserInputService = game:GetService("UserInputService") local player = game:GetService("Players").LocalPlayer local mouse = player:GetMouse() local Selected = script.Selected local ConsumeEvent = game.ReplicatedStorage.Consume local stat = script.Parent:WaitForChild("Stats") local consumables = game.Workspace.Consumables:GetChildren() UserInputService.InputChanged:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseMovement then if mouse.Target then for _, v in pairs(consumables) do if mouse.Target.Name == v.Name then script.Parent.BrickStatus.Main.Visible = true script.Parent.BrickStatus.Main.DisplayName.Text = mouse.Target.Name script.Parent.BrickStatus.Main.Portions.Text = mouse.Target.Portions.Value end end end end end)