Ray not casting?

Hi! I’m making a gun but the Ray isn’t Casting for the gun. Its meant to go on the handle and cast a ray but it doesnt work. I’m not sure exactly what’s going on but here’s the script:

-----Localscript------
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
					local Bullet = Instance.new("Part")
					Bullet.Parent = game.Workspace
					Bullet.Anchored = true
					Bullet.CanCollide = false
					local ray = Ray.new(script.Parent.Handle.CFrame.p,(mouse.hit.p - script.Parent.Handle.CFrame.p).unit * 350)
					local part, position = game.Workspace:FindPartOnRay(ray, char, false, true)

Here is a little clip to show you what’s going on: https://gyazo.com/0a6a5507305f00d3f17fbab913f007ca

Any help is appreciated!

Is it because you never set the CFrame of it?

1 Like

I just set the CFrame of it to the Handle’s but it still doesnt work. Here is a little clip of what’s happening: https://gyazo.com/0a6a5507305f00d3f17fbab913f007ca

We solved the problem in DMs.
The issue was the script wasnt ignoring the previous bullets, so that’s what we did. We created a folder called Bullets, put our bullets in there and set that as the cframe’s ignore list.

Here are the functions we created:
function getHit()
	local ray = Ray.new(script.Parent.Handle.CFrame.p,(mouse.hit.p - script.Parent.Handle.CFrame.p).unit * 600)
	local ignoreList = workspace.Bullets:GetChildren()
	table.insert(ignoreList,char)
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)
	
	return hit, position
end

function fireBullet()
	local hit, position = getHit()
	local distance = (script.Parent.Handle.Position-position).magnitude
	
	local bullet = Instance.new("Part")
	bullet.Size = Vector3.new(.25,.25,distance)
	bullet.CFrame = CFrame.new(script.Parent.Handle.Position, position) * CFrame.new(0,0, -distance/2)
	bullet.Anchored = true
	bullet.CanCollide = false
	bullet.Massless = true
	bullet.Parent = workspace.Bullets
	bullet.Transparency = .4
	
	return bullet
end
Explanation that I sent through dm:

So what I did is I created a folder in the workspace named Bullets

In the code I created a function called getHit() it returns what and where the mouse is currently hitting in game, ignoring any bullets.

how i ignored the bullets was i put the bullets in the bullets folder and used FindPartOnRayWithIgnoreList() to ignore the bullets and the character. you can see this on line 31 of the script.

then i created a function called fireBullet() which does the following:

  1. call getHit() to figure out what and where the mouse is hitting
  2. gets the distance from the bullet and the mouse hit position using magnitude
  3. creates the bullet
  4. changes the bullets size to be the distance from the mouse and the handle of the gun.
  5. sets the bullets cframe to be
    which means: position the bullet at the handle, makes the bullet look at the position the mouse hit, and then move the brick forward half the distance so it is starts at the end of our handle instead of the middle of the handle

There used to be a really nice tutorial that explained how to do this, but it was taken down when workspaceFindPartOnRay() was deprecated. If you want to see that tutorial though, it’s archived online!

Here is how to use the new raycasting api:

2 Likes