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

How would I span through the table in order to get the name of the items in it?

Asked by 5 years ago

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?

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

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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:

  1. Use UserInputService's InputChanged event as Mouse.KeyDown is deprecated/outdated.

  2. Use ServiceProvider:GetService() when indexing services.

Fixed Code:

01local UserInputService = game:GetService("UserInputService")
02 
03local player = game:GetService("Players").LocalPlayer
04local mouse = player:GetMouse()
05local Selected = script.Selected
06local ConsumeEvent = game.ReplicatedStorage.Consume
07local stat = script.Parent:WaitForChild("Stats")
08local consumables = game.Workspace.Consumables:GetChildren()
09 
10UserInputService.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
View all 22 lines...
1
Did not realize players was a service. Thanks. This should work. I didn't know if a loop was the most efficient thing to do here. I appreciate the optimization recommendations. All of my code is from an old project I worked on way way back. This helps. Great work legoguy939 418 — 5y
1
No problem. youtubemasterWOW 2741 — 5y
1
Hey, I did end up using this method. I spent the past 2 or so hours fixing my entire script to UserInputService. In regards to the loop, it caused tons of problems with my script because essentially I was looping everything within it. However, I did learn a lot about loops. But what I was trying to do was basically "cycle through" the table (in theory) or check my table to match mouse.target. legoguy939 418 — 5y
1
What I ended up using was k= table.find(tablename, mouse.target) to match it to a number value, then use an if table[k] then. That worked out perfectly. But for the amount of information I gave, your answer was perfectly acceptable, plus the tips helped. But I just thought I'd let you know about the solution I found. legoguy939 418 — 5y
0
Alright. youtubemasterWOW 2741 — 5y
Ad

Answer this question