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

Name of object only shows on last item of Table?

This is in a local script. What am I trying to achieve here is to make the items in the 'Tags' table to be shown on a UI when your mouse is hovered on said object. Let's say I made 2 door models in the doors folder, with 2 tools on the tools folder. I used this local script in the StarterGui. It worked for the 2nd door and tool but not for the 1st door and tool. It only shows the last item on the table when you hover on it, but it doesn't work for other objects. I hope you get my problem...

--StackEdit added an extra space per line...
   
local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local Tools = game.Workspace.tools:GetChildren() -- Get the tools in the tools folder.

local Doors = game.Workspace.doors:GetChildren() -- Get the doors in the doors folder.

local gui = player.PlayerGui:WaitForChild("toolGui")

local label = gui.Label

local Tags = {}

  

--Putting everything in the Tags table.

  

for i,v in pairs (Tools) do

table.insert(Tags, v)

wait()

end

  

for i,v in pairs (Doors) do

table.insert(Tags, v)

wait()

end

  

--Defining the thing for Tags per class

  

for i,v in pairs (Tags) do

if v:IsA("Tool") then

toolName = v.Name

toolDetect = v:WaitForChild("ClickDetector") -- Assuming it's already there...

end

end

  

for i,v in pairs (Tags) do

if v:IsA("Model") then

doorName = v.Name

doorDetect = v.Door:WaitForChild("ClickDetector") -- Assuming it's already there...

end

end

  

--The problem here is that these parts only call the name of the last item per class in the Tags table.

  

toolDetect.MouseHoverEnter:connect(function(t)

label.Text = toolName

label.Visible = true

mouse.Icon = 'rbxassetid://120192974'

end)

  

toolDetect.MouseHoverLeave:Connect(function()

mouse.Icon = 'rbxassetid://120192974'

label.Visible = false

label.Text = "Label"

end)

  

doorDetect.MouseHoverEnter:connect(function(d)

label.Text = doorName

label.Visible = true

mouse.Icon = 'rbxassetid://120192974'

end)

  

doorDetect.MouseHoverLeave:Connect(function()

mouse.Icon = 'rbxassetid://120192974'

label.Visible = false

label.Text = "Label"

end)

1 answer

✓ Accepted answer

Welp, that's because the toolName variable is changing each time the in pairs loops, and each time it loops its getting changed to the current v that is getting looped, it will stop looping after the last member of the table. So toolName will always be set to the last member.

So you'd have to do something like this:

local Tools = game.Workspace.tools:GetChildren() 
local Tags = {}

for i,v in pairs (Tools) do
	table.insert(Tags, v)
	wait()
end

doorDetect.MouseHoverEnter:connect(function()
    for i, v in pairs (Tags) do 
		label.Text = label.Text .. " " .. tostring(v)
	end
		label.Visible = true
		mouse.Icon = 'rbxassetid://120192974'
end)

That's simply it, what we did is loop all of the Tags table, but not change the text instanly, we added each time a v to the name using .. those 2 points simply link strings together. And I added a " " space to make it cleaner, and I used tostring to turn the v into a string, because v is a tool, so its an istance, and that would error because you can't link instances using ..

And all this will apply to the doors table. Just edit your script a bit. And you can just use one .MouseHoverevent for both of the tools and doors.

And taht's really it, this was tested, and it works

2 comments

0

Ok thanks.

0

no problem

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