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 8 years ago

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

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 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!

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
Ad
Log in to vote
-2
Answered by
FiredDusk 1466 Moderation Voter
8 years ago

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

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

Answer this question