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

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

Asked by 9 years ago

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

01local plr = game.Players.LocalPlayer
02repeat wait() until plr.Character
03local char = plr.Character
04local mouse = plr:GetMouse()
05local items = {
06    "cup",
07    "metal scraps",
08    "bottlecap",
09}
10 
11while wait() do
12    if mouse.Target ~= nil then
13    for _,v in pairs(items) do
14        if mouse.Target.Name:lower() == v then
15            script.Parent.Visible = true
View all 22 lines...

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

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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!

01while wait() do
02    if mouse.Target ~= nil then
03        script.Parent.Visible = false
04        for _,v in pairs(items) do
05            if mouse.Target.Name:lower() == v then
06                script.Parent.Visible = true
07                script.Parent.Text = "[ Press 'E' to pick up "..mouse.Target.Name.." ]"
08            end
09        end
10    end
11end
Ad
Log in to vote
-2
Answered by
FiredDusk 1466 Moderation Voter
9 years ago

Read comment

01local plr = game.Players.LocalPlayer
02repeat wait() until plr.Character
03local char = plr.Character
04local mouse = plr:GetMouse()
05local items = {
06    "cup",
07    "metal scraps",
08    "bottlecap",
09}
10 
11while wait() do
12    if mouse.Target ~= nil then
13    for _,v in pairs(items[1, 2, 3]) do --I hope this works. Try to get all items in the table named "items"
14        if mouse.Target.Name:lower() == v then
15            script.Parent.Visible = true
View all 22 lines...

I hope this helps

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. zaniac10 15 — 9y
0
Still need help... zaniac10 15 — 9y
0
I am at school, when I get home I can. FiredDusk 1466 — 9y
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. FiredDusk 1466 — 9y
0
This doesn't work at all, and there's little reason to believe it would. BlueTaslem 18071 — 9y

Answer this question