Help with raycast damage

hello i am having trouble with getting my damage function to work. Its my first time using raycasting to damage, so i was wondering if im doing something wrong

heres the code i made

--in the server script

gameModule.DealDamage(blade.Position, HRP.CFrame.LookVector, 10, char, 15)

--Damage Module

function module.DealDamage(origin: Vector3,direction: Vector3,rannge: number,char: Model, damage: number)
	local rayParams = RaycastParams.new()
	local hum = char.Humanoid
	local plr = game.Players:GetPlayerFromCharacter(char)
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	rayParams.FilterDescendantsInstances = {char}
	
	local raycast = workspace:Raycast(origin, direction * rannge, rayParams)
	print("working")
	if raycast then
		print("success")
		local hit = raycast.Instance
		local hitChar = hit:FindFirstAncestorOfClass("Model")
		
		if hitChar then
			local hitHum = hitChar:FindFirstChild("Humanoid")
			local hitHRP = hitChar.HumanoidRootPart
			local hitPlr = game.Players:GetPlayerFromCharacter(hitChar)
			if hitHum then
				if hitChar ~= char then
					if char.Health ~= 0 or hitChar.Health ~= 0 and not folders.invinsibles:FindFirstChild(hitChar.Name.."Invinsible") and not folders.counters:FindFirstChild(hitChar.Name.."Counter") and not folders.stuns:FindFirstChild(plr.Name.."Stunned") then
						hitHum:TakeDamage(damage)
						module.Stun(hitChar, 12, 5, 1)
						if hitHum.Health <= 0 then
							print("Humanoid has died")
							hum.Health = hum.MaxHealth
							event:FireAllClients("Heal", char)
							local gui = char.Head:FindFirstChild("GUI Attach").HealthAndStamina:FindFirstChild("Background").Health
							gui:TweenSize(UDim2.new(1,0,1,0))
							TS:Create(gui, TweenInfo.new(.5), {ImageColor3 = Color3.new(0.117647, 1, 0.145098)}):Play()
							if hitPlr then
								plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
							end
						end
					end
					if folders.counters:FindFirstChild(hitChar.Name.."Counter") then
						module.Counter(char, hitChar)
					end
				end
			end
		end
	else
		warn("raycast failed")
	end
	
	
end

heres a video too if its needed

im not sure why the raycast is erroring, help and feedback on my code would greatly be appriciated. thank you for your time :slightly_smiling_face:

1 Like

It would be

local raycast = workspace:Raycast(origin, direction.Unit * rannge, rayParams)

You need to use the Unit of the direction and then multiply it by the distance/range.

1 Like
-- localscript in StarterCharacterScripts

local character = script.Parent
local rootPart = character:WaitForChild("HumanoidRootPart")
local remoteEvent = game.ReplicatedStorage.RemoteEvent

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}

local range = 10

local function DealDamage()
	local raycastResult = workspace:Raycast(rootPart.Position, rootPart.LookVector * range, raycastParams)
	if raycastResult == nil then return end
	local otherCharacter = raycastResult.Instance.Parent
	local player = game.Players:GetPlayerFromCharacter(otherCharacter)
	if player == nil then return end
	remoteEvent:FireServer(otherCharacter)
end
-- server script

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player, character)
	-- security checks
	character.Humanoid:TakeDamage(5)
end)
1 Like

just edited the code, still no luck

I’m pretty sure Position is broken for most tools in characters, so try it with the CFrame’s position:

gameModule.DealDamage(blade.CFrame.Position, HRP.CFrame.LookVector, 10, char, 15)
1 Like

Still not working. Ill look closely at the code to see if there are any things im missing

Is there a reason you aren’t raycasting from the tip to the base of a blade? Surely it would make it more realistic?
However, you could argue that there is a smaller hitbox so less likely you will get a hit
But on the other hand, the knife is in front of the HRP

1 Like

found the issue. Turns out i shouldve made the rays origin the humanoid root part!