Assistance on multiple rays?

Right now, the total damage of the spears is 50; I am using a single ray to detect if it can damage.
How do I make it so each spear does damage only if it hits the part?
Would that be using multiple rays, or how would I do it?


Note how the gif only shows 50, say I wanted like 15 per spear hit, how would I accomplish that?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local spearEvent = ReplicatedStorage:WaitForChild('SpearEvent')
local iceSpear = ReplicatedStorage:WaitForChild('IceSpear')

local maxDistance = 50
local goal = {}
local tweenInfo = TweenInfo.new(0.25)

local damage = 50

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local spearFolder = Instance.new("Folder", character)
		spearFolder.Name = 'SpearFolder'
	end)
end)

local function repeatSpears(player, mouseP)
	for i = 1, 4, 1 do
		local character = player.Character
		local cloned = iceSpear:Clone()
		local based = cloned.Inv
		cloned.Parent = player.Character.SpearFolder
		cloned.Name = "IceSpear"..i
		cloned:SetPrimaryPartCFrame(CFrame.new(character.Head.Position + Vector3.new(math.random(-2, 2), math.random(1, 4), math.random(-2, 2)), mouseP) 
		* CFrame.Angles(math.rad(0), math.rad(-90), 0))
	end
	wait(1)
end

local function spearCreate(player, mouseP, distance, UpperTorso)
	repeatSpears(player, mouseP)
	
	for i, v in pairs(player.Character.SpearFolder:GetChildren()) do
		goal.CFrame = v.Inv.CFrame * CFrame.new(math.random(-1, 1), math.random(-1, 1), -distance + 2.5)
		local tween1 = TweenService:Create(v.Inv, tweenInfo, goal)
		tween1:Play()
		tween1.Completed:Wait()
		Debris:AddItem(v, 2.5)
		
	end
end

spearEvent.OnServerEvent:Connect(function(player, origin, mousePos)
	local spearRay = Ray.new(origin, (mousePos - origin).Unit * maxDistance)
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(spearRay, {player.Character})
	local distance = (origin - mousePos).magnitude
	if distance < maxDistance and hit and hit.Parent:FindFirstChild('Humanoid') then
		spearCreate(player, mousePos, distance)
		hit.Parent.Humanoid:TakeDamage(damage)
	end 
end)
1 Like

You could either cast a ray from each spear, like before it fires OR you could just check the position of the spear? So if (spear.Position - char.HumanoidRootPart.Position).Magnitude <= 1 then it could do damage.

It might be more accurate to just cast a ray from each spear.

1 Like