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

Help with rays? [closed]

Asked by 9 years ago

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)
1
I fixed up the script, and added alternative way of doing same thing. ZarsBranchkin 885 — 9y
0
Thanks. kevinnight45 550 — 9y

Locked by youtubemasterWOW

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
2
Answered by 9 years ago

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)
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) BlueTaslem 18071 — 9y
1
@BlueTaslem You can always do mouse.TargetFilter = DisplayPart ZarsBranchkin 885 — 9y
0
If pos is the vairable for displaypart and it's in the ignore filter then how'd you get the position of it? kevinnight45 550 — 9y
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: kevinnight45 550 — 9y
0
15:00:05.373 - Players.Player1.Backpack.Tool.LocalScript:28: attempt to index global 'DisplayPart' (a nil value) kevinnight45 550 — 9y
Ad