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

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

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.

1 answer

✓ Accepted answer

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:

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)

5 comments

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

1

No problem.

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.

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.

0

Alright.

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