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