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

How do I make this script work for multiple parts named the same?

Hi there,

I've created a local script that is located in the players StarterGui; basically what it does is whenever you hover over any part called 'Door' in the workspace, a GUI becomes visible if you are within a defined distance (the end product will move the part horizontally when you press a key).

I'm having trouble with getting the GUI to appear for multiple parts named 'Door'. Currently I have two parts in my game named 'Door'; however, when moused over, only one of the parts displays the GUI. Nothing happens when I mouse over the other part named door, not even the print("moused over").

I'd really appreciate some help! I've been on this game for several years now but have never really made my own code.

Thanks!

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local door = game.Workspace.Door

distance = 10

function doorOpen()
	
end

function doorLock()
	
end

Mouse.Move:connect(function()
	local target = Mouse.Target
	if target == door then --strange error happening here but still works
		print("moused over")
		if (Player.Character.Torso.Position - door.Position).magnitude <= distance then
			script.Parent.Visible = true;
			print(target.Owner.Value)	
		else
			script.Parent.Visible = false;		
		end
		else
			script.Parent.Visible = false;
	end
end)

2 answers

✓ Accepted answer

Instead of checking whether the moused-over part is the OBJECT game.Workspace.Door, check whether the object has the NAME "Door". When you make the variable door the value game.Workspace.Door, then you are only referring to ONE door.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
--local door = game.Workspace.Door -----don't need this anymore

local distance = 10 --by the way, make all of these local

local function doorOpen()

end

local function doorLock()

end

Mouse.Move:connect(function()
	local target = Mouse.Target
	if not target then --checks to see whether "target" returns nil
		if target.Name == "Door" then
	        	print("moused over")
	        	if (Player.Character.Torso.Position - target.Position).magnitude <= distance then
	            		script.Parent.Visible = true --you don't need the semicolon; that's javascript, not lua
	            		print(target.Owner.Value)   
	        	else
	            		script.Parent.Visible = false
	        	end
	        else
	        	script.Parent.Visible = false -- you indented this wrong
	    	end
	end
end)

3 comments

0

This seems to have worked, thanks! But I'm getting an error whenever I'm not moused over a door: Players.Player1.PlayerGui.DoorGui.Frame.DoorScript:17: attempt to index local 'target' (a nil value)

0

This is because when you're not pointing at anything, "target" returns nil. All you need to do is add another "if...then" statement to check whether it is nil.

0

It works now, thank you!

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