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

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

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.

1 comment

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).

2 answers

✓ Accepted answer

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.

2 comments

0

Okay thank you, I will try this.

0

It worked! thanks!

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

1 comment

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.

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