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

This script seems like it should work, can anyone tell me why it won't?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
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.

0
The main Errors I see within the code are lines 7 and 15; You are attempting to compare 'obj.Name' (A String Value) to 'acceptable_objects' (A Table Value). TheeDeathCaster 2368 — 9y

2 answers

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

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.

0
Okay thank you, I will try this. rollercoaster57 65 — 9y
0
It worked! thanks! rollercoaster57 65 — 9y
Ad
Log in to vote
-2
Answered by
Paldi 109
9 years ago

you should maybe make it "local acceptable_objects = {"TinyGun", "Crates"}" ? or try removing the "_"

0
Doing the '_' is fine within a Variable, as long as he/she understands it; That is not the Problem. Also, adding the 'local' only makes it 'local' to the code (like a function for instance), it does efficient code, but, that is not the problem either. TheeDeathCaster 2368 — 9y

Answer this question