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

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

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

5 comments

0

Hmm wheres the code block for the particle?

0

I Noticed The After The if statement for Hit You Only told the script to print ("intercepted!") Is That Intentional?

0

its just a small ball in the workspace with a particle in it. I didn't use a script to create it

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

0

ive also tried the script without the particle emiter part

1 answer

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

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