How can I get :Touched event to work on a raycast beam?

In my game, I want to make my gun so that whenever the beam created via raycast touches the player, a value is added so that once the player dies the leaderboard can trace back who killed who and add a value for KOs. However, the touched event seems to never trigger even though the beam touches the npc. I can’t seem to find anything on the developer resources since my situation is unique. How can I get it to work? What am I doing wrong? Help!

– gun handler

local attacker = Instance.new('StringValue')

	attacker.Name = 'Attacker'

	attacker.Parent = beam

	attacker.Value = player.Name
	
	local damageScript = ServerStorage:FindFirstChild('Damage'):Clone()

	damageScript.Parent = beam
	
	local distance = (Handle.Position - targetlocation).magnitude
	beam.Size = Vector3.new(0.1, 0.1, distance)
	beam.CFrame = CFrame.new(Handle.Position, targetlocation) * CFrame.new(0,0, -distance/2)

	game.Debris:AddItem(beam, 0.1)
	local newray = RaycastParams.new()
	local raydirection = (targetlocation - Handle.Position) * range --put a number here for the distance

	newray.FilterDescendantsInstances = {player.Character}
	-- hit detection handling
	local result = workspace:Raycast(Handle.Position, raydirection, newray)
	if result then
		if result.Instance then
			--print(result.Instance)
			if result.Instance.Parent:FindFirstChild("Humanoid") then
				local num = math.random(1,10)
				if num > 8 then
					print("miss")
					print(num)
				else
					if result.Instance.Name == "Head" then
						print(result.Instance.Parent.Head.Name)
						result.Instance.Parent.Humanoid.Health -= headshot
					else
						print(result.Instance.Name)
						result.Instance.Parent.Humanoid.Health -= damage
					end
					if result.Instance.Parent.Humanoid.Health <=10 then
						print("low health")
						

					end
				end
				
			end
		end

– damagescript

local bullet = script.Parent
local hitsound = game.Workspace.Click
local headshot = game.Workspace.Headshot

local function player_check(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if humanoid and humanoid.Parent.Name ~= bullet.Attacker.Value then
		--if otherPart.Name == "Head" then
		--	humanoid:TakeDamage(35)
		--	headshot:Play()
		--else
		--	humanoid:TakeDamage(10)
		--	hitsound:Play()
			
		--end
		print("bullet connected")
		local player = game.Players:FindFirstChild(humanoid.Parent.Name)
		if player then
			local tag = player:FindFirstChild("Killer")
			if tag then
				tag.Value = bullet.Attacker.Value
				bullet:Destroy()
			end
		end
	end
end

bullet.Touched:Connect(player_check)

–leaderboard handle

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			AmmoDrop(character.HumanoidRootPart.Position)
			HealthDrop(character.HumanoidRootPart.Position)

			game.Workspace.Camera.FieldOfView = 70
			player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
			local killerTag = game.Players:FindFirstChild(player.Killer.Value)
			if killerTag then
				killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1
				
			end
		end)
	end)
end)

I found this article which may help:

Intro to Raycasting

It talks about hit detection near the bottom.

I’ve tried using raycastresult.Instance but the function does not seem to trigger if I place it inside of the corresponding function (gun handler script at the end)

1 Like