So I just created this gun using RayCast, and it works really well. The problem is though, the actual 'laser' doesn't actually detect when it hit a humanoid model.

Here's the portion of the script that is related to RayCast(LocalScript in Tool)

local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
				
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("White")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false

local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)


for i = 0,1,.1 do
	beam.Transparency = i
	wait()
end

game:GetService("Debris"):AddItem(beam, 0.1)

And here's the portion that is related to the RayCast detecting the humanoid model:

if part then
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		game.StarterPlayer.Hit:Play()
		player.PlayerGui:WaitForChild('Hitmarker').Enabled = true
		humanoid:TakeDamage(26)
		wait()
		player.PlayerGui:WaitForChild('Hitmarker').Enabled = false
	end
end

No errors in output, and I'm also new to this stuff. Can anyone tell me why this is happening?

Regards, Sensei_Developer

2 comments

0

is this the full script? If so you are missing a touched event

0

@voidofdeathfire I didn't use .Touched because I knew there would be some issues regarding the function. .Touched is notoriously hunky for scripts and sometimes very unreliable. Thanks for commenting anyways!