I am trying to create a ray that goes in the direction of MoveDirection. I am using MoveDirection simply because it's based on the player's input rather than where the player is going. There aren't any errors but the ray is detecting parts when a part isn't even there!
Here's the script
local LocalPlayer = game.Players.LocalPlayer local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() local RootPart = Character:WaitForChild("HumanoidRootPart") local Mouse = LocalPlayer:GetMouse() local AttackRay local Hit,Pos local IgnoreList = Character:GetChildren() table.insert(IgnoreList,workspace.Bullet) table.insert(IgnoreList,workspace.Environment.Ground) game:GetService("RunService").RenderStepped:connect(function() AttackRay = Ray.new(RootPart.Position,(Character:WaitForChild("Humanoid").MoveDirection*15)+ RootPart.Position) Hit,Pos = game.Workspace:FindPartOnRayWithIgnoreList(AttackRay,IgnoreList) if Hit then print("Intercepted!") end end)
I am using a part that emits particles to show the direction of the ray so I can get a Visual representation of the rays Direction
Here is a clip of the problem (There is an error but it is a load error for a decal which is irrelevant)
https://streamable.com/2sjm3d
Is there something wrong with my lines of code? Any help is appreciated
The reason why it didn't detect properly was that the ray's direction was set to the wrong value.
Credits go to TimScript on Discord
local LocalPlayer = game.Players.LocalPlayer local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait() local RootPart = Character:WaitForChild("HumanoidRootPart") local Mouse = LocalPlayer:GetMouse() local AttackRay local Hit,Pos local IgnoreList = Character:GetChildren() local part table.insert(IgnoreList,workspace.Bullet) table.insert(IgnoreList,workspace.Environment.Ground) game:GetService("RunService").RenderStepped:connect(function() AttackRay = Ray.new(RootPart.Position,(Character:WaitForChild("Humanoid").MoveDirection*15)) Hit,Pos = game.Workspace:FindPartOnRayWithIgnoreList(AttackRay,IgnoreList) workspace.Bullet.Position = AttackRay.Direction+RootPart.Position if Hit then print("Intercepted!") end end)
Thx to anyone who tried to solve the problem