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

Help with rays?

I was trying to make a part called "DisplayPart" follow the mouse every time it moves but it won't I think because of either the Move event or the ray(Line 20-23),why and how do I fix it? I don't have any errors in the output.

Script:

Tool = script.Parent
Handle = Tool.Handle
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
---------------------------------
function CreateDisplayPart()--Creates DisplayPart
	local DisPart = Instance.new("Part",Camera)--Hides it from players
		DisPart.Anchored = true
		DisPart.CanCollide = false
		DisPart.Transparency = 0.5
		DisPart.Name = "DisplayPart"
	end
---------------------------------
	Tool.Equipped:connect(function(Mouse)
		---------------------------------
DisplayPart = Camera:FindFirstChild("DisplayPart")
		if not DisplayPart then--If not found then makes it
			CreateDisplayPart()
		else
			Mouse.Move:connect(function()
				
				local Ray = Ray.new(Mouse.UnitRay.Origin,--Creates ray
							     Mouse.UnitRay.Direction)

				Hit = Workspace:FindPartOnRay(Ray)
			if Hit ~= nil then --If not nil then do the following
				DisplayPart.Position = Mouse.UnitRay.Direction
				end
			end)
		end
	end)

2 comments

1

I fixed up the script, and added alternative way of doing same thing.

0

Thanks.

1 answer

✓ Accepted answer

The problem is that your ray is only 1 stud long, so it doesn't hit anything. Also, why are you casting a ray, when you can already use mouse.Hit, to place the part? Anyway, here is fixed code:

Tool = script.Parent
Handle = Tool.Handle
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
---------------------------------
function CreateDisplayPart()--Creates DisplayPart
    local DisPart = Instance.new("Part",Camera)--Hides it from players
        DisPart.Anchored = true
        DisPart.CanCollide = false
        DisPart.Transparency = 0.5
        DisPart.Name = "DisplayPart"
	DisplayPart = DisPart
    end
---------------------------------
    Tool.Equipped:connect(function(Mouse)
        ---------------------------------
DisplayPart = Camera:FindFirstChild("DisplayPart")
        if not DisplayPart then--If not found then makes it
            CreateDisplayPart()
        end
         Mouse.Move:connect(function()

                local Ray = Ray.new(Mouse.UnitRay.Origin,--Creates ray
                                 Mouse.UnitRay.Direction * 900) -- Ray will go 900 studs as max

                Hit, pos = Workspace:FindPartOnRay(Ray, DisplayPart) -- Also gotta add the part to ignore filter
            if Hit ~= nil then --If not nil then do the following
		-- Use CFrame here, or else the DisplayPart will get placed on top of the intersecting part.
                DisplayPart.CFrame = CFrame.new(pos) -- Direction doesn't tell where the ray hit the ground
                end
            end)
    end)

Edit: Fixed up the script. And you ignore the part itself, so you ray wouldn't stop at the part, but at things behind it. Also, here is the alternative way of doing same thing:

Tool = script.Parent
Handle = Tool.Handle
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
---------------------------------
function CreateDisplayPart()--Creates DisplayPart
    local DisPart = Instance.new("Part",Camera)--Hides it from players
        DisPart.Anchored = true
        DisPart.CanCollide = false
        DisPart.Transparency = 0.5
        DisPart.Name = "DisplayPart"
	DisplayPart = DisPart
    end
---------------------------------
    Tool.Equipped:connect(function(Mouse)
        ---------------------------------
DisplayPart = Camera:FindFirstChild("DisplayPart")
        if not DisplayPart then--If not found then makes it
            CreateDisplayPart()
        end
         Mouse.Move:connect(function()
		mouse.TargetFilter = DisplayPart
            if mouse.Target then --If not nil then do the following
                DisplayPart.CFrame = CFrame.new(mouse.Hit.p)
                end
            end)
    end)

5 comments

1

Ray is really a bad name for them... If you simply use mouse.Hit.p make sure the object is part of the player's character, so that it isn't one of the things the mouse's Hit collides with (it would come quickly closer towards the camera otherwise)

1

@BlueTaslem You can always do mouse.TargetFilter = DisplayPart

0

If pos is the vairable for displaypart and it's in the ignore filter then how'd you get the position of it?

0

and I changed both the direction of the ray to Hit.p and the origin like BlueTaslem said.Another question how do I make the part go above antoher part?,I also got a error saying:

0

15:00:05.373 - Players.Player1.Backpack.Tool.LocalScript:28: attempt to index global 'DisplayPart' (a nil value)

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