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?
01 | player = game.Players.LocalPlayer |
02 | mouse = player:GetMouse() |
03 | Selected = script.Selected |
04 | ConsumeEvent = game.ReplicatedStorage.Consume |
05 | stat = script.Parent:WaitForChild( "Stats" ) |
06 | local consumables = game.Workspace.Consumables:GetChildren() -- table here |
07 | mouse.Move:connect( function () |
08 |
09 | if mouse.Target ~ = nil then |
10 | if mouse.Target.Name = = consumables [ WhatDoIPutHere ] .Name then -- main problem here |
11 |
12 | script.Parent.BrickStatus.Main.Visible = true |
13 | script.Parent.BrickStatus.Main.DisplayName.Text = mouse.Target.Name |
14 | 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:
01 | local UserInputService = game:GetService( "UserInputService" ) |
02 |
03 | local player = game:GetService( "Players" ).LocalPlayer |
04 | local mouse = player:GetMouse() |
05 | local Selected = script.Selected |
06 | local ConsumeEvent = game.ReplicatedStorage.Consume |
07 | local stat = script.Parent:WaitForChild( "Stats" ) |
08 | local consumables = game.Workspace.Consumables:GetChildren() |
09 |
10 | UserInputService.InputChanged:Connect( function (input) |
11 | if input.UserInputType = = Enum.UserInputType.MouseMovement then |
12 | if mouse.Target then |
13 | for _, v in pairs (consumables) do |
14 | if mouse.Target.Name = = v.Name then |
15 | script.Parent.BrickStatus.Main.Visible = true |