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

[Solved!] Why Isn't My Ray Detecting Properly?

Asked by 4 years ago
Edited 4 years ago

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

0
Hmm wheres the code block for the particle? crueluu 169 — 4y
0
I Noticed The After The if statement for Hit You Only told the script to print ("intercepted!") Is That Intentional? crueluu 169 — 4y
0
its just a small ball in the workspace with a particle in it. I didn't use a script to create it Oblivious_Blitz 1 — 4y
0
Yes. i didn't do much on the script since it wasn't working. I was going to add on to it when i resolved the issue Oblivious_Blitz 1 — 4y
0
ive also tried the script without the particle emiter part Oblivious_Blitz 1 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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

Ad

Answer this question