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

Error With Mouse/Door Script?

The point of the script is to allow a player to put there mouse over a part named door, then make the door invisible and cancollide, but nothing happens, No errors. Any idea why?

local mouse = game.Players.LocalPlayer:GetMouse()
local mousepos = mouse.hit.p
local target = game.Workspace.Door

if mousepos == target.Position then
	target.Transparency = 1
	target.CanCollide = false
end

2 answers

✓ Accepted answer

Because you store a single vector3 position in 1 variable which your mouse will probably never hit again. You really don't even need to check the position. You can use a property of mouse called Target. It returns the name of the part your mouse is pointing at; well that's what it prints but it also holds the instance object of it allowing you to travel through its hierarchy. So you can create a method like so:

local mouse = game.Players.LocalPlayer:GetMouse()
local target = game.Workspace.Door

mouse.Move:connect(function() -- an event for mouse the triggers each time the mouse moves
	if mouse.Target == "Door" then -- if what you point at is named door then do this
		target.Transparency = 1;
		target.CanCollide = false;
	end
end)

Just the word door may be vague. As long as you don't have doors that you don't want people going through then you should be fine. Otherwise you would want to change the name of the others or use a separate method of checking where the part is in hierarchy if you organize the doors better. ( :IsDescendantOf() ).

The reason this doesn't work-

if mouse.hit.p == game.Workspace.Door.Position then

is because in order for it to work the mouse has to be on the exact position of the center of the door. Instead, use the Target property and check if the mouse's Target is the door. Like this:

if mouse.Target == game.Workspace.Door then

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