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

How come this GUI only pops up for one item in the table?

The GUI will only pop up if the item the mouse hovers over is at the bottom of the 'items' table. Script:

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
local mouse = plr:GetMouse()
local items = {
	"cup",
	"metal scraps",
	"bottlecap",
}

while wait() do
	if mouse.Target ~= nil then
	for _,v in pairs(items) do
		if mouse.Target.Name:lower() == v then
			script.Parent.Visible = true
				script.Parent.Text = "[ Press 'E' to pick up "..mouse.Target.Name.." ]"
			else
				script.Parent.Visible = false
			end
		end
	end
end

It will work if my mouse hovers over an item named "Bottlecap", but if I hover over an item named "Metal Scraps", it won't. Help?

2 answers

There's something subtle about your code. Let's step through it for "bottlecap" first:


1.a. if target is "cup" then

1.b. (it's not)

1.c. else

1.d. hide gui

2.a. if target is "metal scraps" then

2.b. (it's not)

2.c. else

2.d. hide gui

3.a. if target is "bottlecap" then

3.b. show "pick up " .. "Bottlecap"

3.c. else

3.d. (it was)


Now compare this to running through with "metal scraps"


1.a. if target is "cup" then

1.b. (it's not)

1.c. else

1.d. hide gui

2.a. if target is "metal scraps" then

2.b. show "pick up " .. "Metal Scraps"

2.c. else

2.d. (it was)

3.a. if target is "bottlecap" then

3.b. (it's not)

3.c. else

3.d. (hide gui)


Notice the problem? For each name, you take a definitive action -- either show or hide -- thus the only one that ends up mattering is the last one.


You only want to make it invisible when none match.

A simple way to accomplish that is start by hiding it, then just show it when you put text:

Also, tab your code properly!

while wait() do
	if mouse.Target ~= nil then
		script.Parent.Visible = false
		for _,v in pairs(items) do
			if mouse.Target.Name:lower() == v then
				script.Parent.Visible = true
				script.Parent.Text = "[ Press 'E' to pick up "..mouse.Target.Name.." ]"
			end
		end
	end
end

Read comment

local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local char = plr.Character
local mouse = plr:GetMouse()
local items = {
    "cup",
    "metal scraps",
    "bottlecap",
}

while wait() do
    if mouse.Target ~= nil then
    for _,v in pairs(items[1, 2, 3]) do --I hope this works. Try to get all items in the table named "items"
        if mouse.Target.Name:lower() == v then
            script.Parent.Visible = true
                script.Parent.Text = "[ Press 'E' to pick up "..mouse.Target.Name.." ]"
            else
                script.Parent.Visible = false
            end
        end
    end
end

I hope this helps

5 comments

0

Unfortunately, that didn't work... also, I don't think this would be practical because you would have to add a number to every item I add into my game.

0

Still need help...

0

I am at school, when I get home I can.

0

I see! U have just strings in the table. You need to parent them or find the parent. I hope you know what I mean.

0

This doesn't work at all, and there's little reason to believe it would.

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