plr = game.Players.LocalPlayer mou = plr:GetMouse() acceptable_objects = {"TinyGun", "Crates"} mou.Move:connect(function() print('moved') obj = mou.Target if obj.Name == acceptable_objects then print('We have a target!') print(obj) plr.PlayerGui.desc.descs.Visible = true plr.PlayerGui.desc.descs.Text = obj.Name plr.PlayerGui.desc.descs.Position = UDim2.new(0,mou.X,0,mou.Y) end if obj.Name ~= acceptable_objects then plr.PlayerGui.desc.descs.Visible = false end end)
There is no error, it runs right, except it will not work at line 7, it basically just stops there. The script runs each time the mouse is moved, but it will not run the conditional statement. I know this because I already removed line seven, and then it worked. It actually worked perfectly.
First, use local
variables! It's good for your health -- it will come to bite you later if you don't. This applies to the variables defined inside functions, like obj
. ROBLOX's Script Analysis should already be complaining about this to you.
What does ==
mean? It means are these the same. So is what your mouse hovering over ever going to be a list of names? No -- it's going to be some part in the world.
The real question you want to ask is if the name of obj
is listed in the list acceptable_objects
. Unfortunately, Lua doesn't provide a way to ask if a list contains something, so we have to write our own using a loop. find
and contains
are basically the same function, so I'll write find
here:
function find(tab, val) for i, v in pairs(tab) do if v == val then -- HERE is where the == belongs -- when we're looking at INDIVIDUALS things IN the list return i end end end ------ -- (other code) if find( acceptable_targets, mou.Target.Name ) then
You should also make sure obj
slash mou.Target
is not nil
(this happens when you point at the sky).
Another option is to use a dictionary instead of a list, which would let us not make the find
function:
local acceptable = { TinyGun = true, Crates = true } -- (other code) if acceptable[ mou.Target.Name ] then
Though this adds the burden of = true
being appended to each thing you want to include.
you should maybe make it "local acceptable_objects = {"TinyGun", "Crates"}" ? or try removing the "_"