How to create a part to show the raycast?

Hey i’m DevFoll and i wanna create a gun that requires raycasting i’ve created the raycast and i wanna add an effect like this
https://cdn.discordapp.com/attachments/938894648062124033/938992419763011614/2022-02-03_21-59-34.mp4 But i don’t know how to create that i almost did it but not complety good, I don’t have the best understanding in raycasting so i came here to ask you guys
Video file was to big so had to get the link, clicking the link will install the video i tested
This is my server code:

**script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player, MousePos, MouseCFrame)
	local Anim = Player.Character:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Animations.Shooting)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Player.Character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast = game.Workspace:Raycast(script.Parent.Part.Position,( MousePos - script.Parent.Part.Position)*300, Params)
	if Raycast then
		local CharHit = Raycast.Instance:FindFirstAncestorOfClass("Model")
		local Smoke = game.ReplicatedStorage.Smoke:Clone()
		local Particle = game.ReplicatedStorage.ParticleEmitter:Clone()
		Particle.Parent = script.Parent.Part
		Smoke.Parent = script.Parent.Part
		script.Parent.Part.PointLight.Enabled = true
		script.Parent.Ammo.Value -= 1
		script.Parent.Handle.fire:Play()
		Anim:Play()
		local Bullet = game.ReplicatedStorage.Bullet:Clone()
		print(MousePos)
		wait(0.3)
		script.Parent.Part.PointLight.Enabled = false
		Particle:Destroy()
		Anim:Stop()
		local s,e = pcall(function()
		if (MousePos - script.Parent.Part.Position).Magnitude < 30 then
			if Raycast.Instance.Name == "Head" and CharHit:FindFirstChild("Humanoid") then
					CharHit.Humanoid.Health = 0
				elseif CharHit:FindFirstChild("Humanoid") then
					CharHit.Humanoid.Health -= 50
					
			end
			end
		end)
		wait(0.8)
		Smoke:Destroy()
	end
end)**

And this is my client code: **local Mouse = game.Players.LocalPlayer:GetMouse()
local Debounce = false
local UIS = game:GetService("UserInputService")
script.Parent.Activated:Connect(function()
	if Debounce == false and script.Parent.Ammo.Value > 0 then
		Debounce = true
	script.Parent.RemoteEvent:FireServer(Mouse.Hit.Position, Mouse.Hit)
		wait(1.2)
		Debounce = false
	elseif Debounce == false and  script.Parent.Ammo.Value <= 0 then
		script.Parent.NeedsAmmo:FireServer()
	end
	
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.R and script.Parent.Parent == game.Players.LocalPlayer.Character then
		script.Parent.Reload:FireServer()
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift and game.Players.LocalPlayer.Character.Humanoid.WalkSpeed < 24 then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24
	elseif Input.KeyCode == Enum.KeyCode.LeftShift and game.Players.LocalPlayer.Character.Humanoid.WalkSpeed >= 24 then
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
	end
end)**
``` In short i wanna make a part that has the size of 0.3 square and is long enough to show from the handle of the gun to the endmousepos.
1 Like

If you’re talking about how to make the instant bullet appear, that’s actually very easy!

First of all, you will have to calculate the distance between the two points, the gun’s barrel (or where you want the bullet to start from) to the character’s raycasted part.

Distance = (Raycast.Position - Barrel.Position).magnitude

After we got the distance, we’ll also need the direction which is basically the same calculation but without magnitude.

Direction = (Raycast.Position - Barrel.Position)

And last, we’re gonna need the MiddlePoint
Imgur
Let’s say the middle point here is the orange dot/x, it would be equal to the Distance/2

MiddlePoint = Barrel.Position + Direction/2

Now, we will have to make a part for the “laser”.

local Laser = Instance.new("Part",workspace)
Laser.Name = "Laser"
Laser.CFrame = CFrame.new(MiddlePoint,Raycast.Position) -- We position the Laser at the middle point, facing the raycast position
-- The size is gonna be equal to the distance on the Z axis.
Laser.Size = Vector3.new(1,1,Distance)
Laser.Material = Enum.Material.Neon
-- You can add any other customization

This should be it! Pretty simple right? I hope this script works, it’s 3:42 AM and I am writing this without the script editor so I hope there are no errors. If you need any more help feel free to reply to this message. :slightly_smiling_face:

Edits: Some writing mistakes!

8 Likes

thx im pretty new to ray casting and this is exactly like i wanted but i got a error Players.DevFoll.Backpack.Shotgun.Script:24: invalid argument #1 to ‘new’ (Vector3 expected, got number)

1 Like

Oh sorry! I wrote Vector.new(), it should be Vector3.new()

I edited the script, I hope it works now!

1 Like

Doesn’t work i still get the same errormessage (Vector3 expected, got number)

1 Like

So i changed it to this

local Laser = Instance.new("Part",workspace)
				Laser.Name = "Laser"
				Laser.CFrame = CFrame.new(script.Parent.Part.Position, MousePos) 
				
				Laser.Size = Vector3.new(1,1,Distance)
				Laser.Material = Enum.Material.Neon
				Laser.Anchored = true

and now it looks like this

1 Like

Hello! Sorry for the mistakes MiddlePoint isn’t Distance/2, it’s

MiddlePoint = Barrel.Position + Distance/2

And then

Laser.CFrame = CFrame.new(MiddlePoint, MousePos)

I will also update the whole script and make it work.

This is a new error that pops up “attempt to perform arithmetic (add) on Vector3 and number”

1 Like

Sorry, once again, it wasn’t supposed tu be Distance/2, it was supposed to be Direction/2 :sweat_smile:

I will edit the script again. Sorry!!

I get no errors but the bullets is shooting the oppisite way i want it to.
image

That’s because he told you to calculate the direction using (start - end) when it should be (end - start). Change the direction variable’s value to (Raycast.Position - Barrel.Position)

1 Like

Oh, yeah sorry, once again. I haven’t tested the script, and as I said it was written at almost 4 AM without the script editor. :sweat_smile: I will change the script. It should be instead

Distance = (Raycast.Position - Barrel.Position).magnitude
1 Like

thank you it worked hhhhhhhhhhh