First time making a raycast gun, player not taking damage

Hello devs! I am currently creating my first raycast gun and it had ran into an error that does not display in the output…

I am unsure whether the ray is even being created. What it is doing is not damaging the player…

Here is my code:

local replicatedStorage = game:GetService("ReplicatedStorage")
local fireBullet = replicatedStorage:WaitForChild("FireBullet")

local tool = script.Parent

fireBullet.OnServerEvent:Connect(function(player, damagePerHit)
	local rayOrigin = tool.Muzzle.Position
	local rayDestination = Vector3.new(0, 0, 100)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDestination, raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		
		hitPart.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local humanoid = hit.Parent.Humanoid
				
				humanoid:TakeDamage(damagePerHit)
			end
		end)
	end
end)

Help would be appreciated!:slightly_smiling_face:

(The script is a server script)

1 Like

New development: When I try printing the ray it prints ‘nil’ if that means anything.

For this you don’t use the touched methode. Instead, you say:

if hitpart.Parent:FindFirstChild("Humanoid") then
    local humanoid = hitpart.Parent.Humanoid		
    humanoid:TakeDamage(damagePerHit)
end

Implemented this in you’re code, it would look lile this:

local replicatedStorage = game:GetService("ReplicatedStorage")
local fireBullet = replicatedStorage:WaitForChild("FireBullet")

local tool = script.Parent

fireBullet.OnServerEvent:Connect(function(player, damagePerHit)
	local rayOrigin = tool.Muzzle.Position
	local rayDestination = Vector3.new(0, 0, 100)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDestination, raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		
        if hitpart.Parent:FindFirstChild("Humanoid") then
           local humanoid = hitpart.Parent.Humanoid		
           humanoid:TakeDamage(damagePerHit)
        end
	end
end)

This is because the raycast doesn’t need a touched methode, it already checks for the object by itselfs.

1 Like

It still inflicts no damage to any humanoids.

Wait I am getting a new error now.

Unable to cast Vector3 to float

Error line: humanoid:TakeDamage(damagePerHit)

That isn’t a problem with your raycast, but with your remote event, could you send me the line in the localscript when you fire the event?

tool.Activated:Connect(function()
	if not isReloading and not fireDebounce then
		
		currentAmmo = currentAmmo - 1
		print(currentAmmo)
		
		text.Text = currentAmmo .. "/" .. ammoCapacity
		
		print(mouse.Hit.p)
		
		if currentAmmo ~= 0 then
			fireBullet:FireServer(mouse.Hit.p, damagePerHit) <--
		else
			print("No more ammo")
			isReloading = true
		end
	end
end)

And the damagePerHit variable is set to 5.

Ah I see the problem, you are firing the mouse position and the damage, but in your server script you only have the damage. Look:

fireBullet.OnServerEvent:Connect(function(player, damagePerHit) -- Only damage per hit

It should be:

fireBullet.OnServerEvent:Connect(function(player, mousePosition, damagePerHit) -- Only damage per hit

1 Like

Bruh, how did I not notice that.

1 Like

Lol it happened countless times to me, I hope this helped.

1 Like