How to make a gun shot line display?

I’m trying to get the gun tool I made to have a line that goes from the gun to the target.

Reference:
Reference1

I’m not sure how to get this to work without raycasting

I tried finding a tutorial for my scenario.

Here’s the server-sided code (client sided code is literally just detecting the target of the click)

local Players = game:GetService("Players")

local Friendly = script.Parent:FindFirstChild("Friendly").Value
local Damage = script.Parent:FindFirstChild("Damage").Value
local Sound = script.Parent.Pistol.Pistol.Fired

script.Parent.Activation.OnServerEvent:Connect(function(plr, target)
	
	Sound:Play()
	
	if Players:GetPlayerFromCharacter(target.Parent) ~= nil and Friendly == true then return end
	
	if target:IsDescendantOf(plr.Character) then
		print("Can't damage own player")
	else
		if target.Parent:FindFirstChildWhichIsA("Humanoid") then
			if target.Name == "Head" then
				target.Parent.Humanoid.Health -= Damage * 2
			else
				target.Parent.Humanoid.Health -= Damage				
			end
		end
	end
	
end)

Thanks

If your gun is raycast based then you can use this code (for raycasting visualization):

3 Likes

It isn’t using raycast, although if anyone can change the gun to be raycast based then it would work, but right now it’s just using the target of the mouse click.

1 Like

Well if it have an origin and the final position then you can still use it.
replace result.Position = your mouse.Hit
origin = where you fire
distance = (origin-position).Magnitude

1 Like

Tho I am still recommended you to switch from raycasting instead of using mouse.Hit. Its will help you later on customization and accuracy

1 Like

distance = (origin-position).Magnitude

what is position in this context?

mouse.Hit.Position
character litmit stuff

1 Like

is this right?

local result = workspace:Raycast(EndPart)

			if result then
				local distance = (EndPart.Position - target.Position).Magnitude
				local p = Instance.new("Part")
				p.Anchored = true
				p.CanCollide = false
				p.Size = Vector3.new(0.1, 0.1, distance)
				p.CFrame = CFrame.lookAt(EndPart, target.Position)*CFrame.new(0, 0, -distance/2)
			end

mouse.hit = target right

If you dont use raycast now then remove the if statement and the variable result of the raycast above.

1 Like

It works but it goes to the center of the part it hits because i use .position

Use mouse.Hit.Position instead of (your part that hit).Position

1 Like

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