Raycast not being able to pick up the instance it is colliding with all of the time

  1. What do you want to achieve? Detecting if the ray has hit an object

  2. What is the issue? Sometimes it picks up on the player’s object, when I want it to constantly pick up on the object

  3. What solutions have you tried so far? Looking on the devforum where I found the solution to my original issue, but I cannot find a fix to this issue

The script is down below. (make sure to replace the “PUT YOUR PLAYERS NAME HERE” part with your roblox username

local beam = Instance.new("Beam")
local part = Instance.new("Part")
part.CanCollide = false
part.Transparency = 1
part.Parent = game.Workspace
part.Anchored = true
part.Size = Vector3.new(0,0,0)
local att = Instance.new("Attachment")
att.Parent = part
local part2 = Instance.new("Part")
part2.Parent = game.Workspace
part2.CanCollide = false
part2.Transparency = 1
part2.Anchored = true
part2.Size = Vector3.new(0,0,0)
local att2 = Instance.new("Attachment")
att2.Parent = part2
beam.Parent = script.Parent
beam.Attachment0 = att
beam.Attachment1 = att2

repeat wait() until game.Players:WaitForChild("PUT YOUR PLAYERS NAME HERE")

wait(1)

while true do
	wait()
	local ray = workspace:Raycast(script.Parent.Position, game.Players.dappa2000.Character.HumanoidRootPart.Position)
	if ray ~= nil then
		print(ray.Instance.Name)
	end
	part.Position = script.Parent.Position
	part2.Position = game.Players.dappa2000.Character.HumanoidRootPart.Position
end


This hurts my eyes while reading, but if you meant that it collides with the player, then you should use raycastparams with exclude and put the character inside of the argument.

1 Like

By the way dont use deprecated wait(), use task library and task.wait() instead

1 Like

Oh sorry, I was trying to say that I need to see if it is colliding with the player, and if it isn’t to pick up on that. Sorry, sometimes I can yap on about nonsense :sweat_smile:

could you please send a video so i can understand it better

ok wait i see the problem:
raycast has 3 arguments:

  • start
  • direction
  • raycastparams (we dont need this)
    the problem is that your direction is just the rootpart position, but i should be:
    local direction = rootpart.position - start position if i am correct.

I will upload it in a second (sorry it’s taking a while I have to compress the file :sweat_smile:)

Replace:

local ray = workspace:Raycast(script.Parent.Position, game.Players.dappa2000.Character.HumanoidRootPart.Position)

With:

local ray = workspace:Raycast(script.Parent.Position, game.Players.dappa2000.Character.HumanoidRootPart.Position - script.Parent.Position)
1 Like

You are a genius. Thanks alot! (sorry I couldn’t upload that video, I couldn’t find a way to compress it)

No problem, glad I could help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.