so ive been making a sword combat system and i am using raycasting too detect collisions but i dont know a way too make the ray stay on the sword so i ended up just casting a bunch of rays every wait() but the problem is that its very inaccurate so is there a better way to do this?

script

for i = 1,8,1 do
	wait()

	local ray = Ray.new(script.Parent.Position, (script.Parent.CFrame.lookVector).unit*4)
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent, script.Parent.Parent.Head, script.Parent.Parent["Left Arm"], script.Parent.Parent["Left Leg"], script.Parent.Parent["Right Leg"], script.Parent.Parent.Torso, script.Parent.Parent.HumanoidRootPart})
	if hit then
		if hit.Name == "Head" or hit.Name == "Torso" or hit.Name == "Right Arm" or hit.Name == "Left Arm" or hit.Name == "Left Leg" or hit.Name == "Right Leg" or hit.Name == "HumanoidRootPart" and hit.Parent.Hit.Value == false then
			game.ReplicatedStorage.Events.KIllerEvent:FireServer(hit)
			print(hit.Name .. " Was Hit!")
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 13
			hit.Parent.Humanoid.MaxHealth = hit.Parent.Humanoid.MaxHealth - 4
			wait(0.1)
			hit.Parent.Hit.Value = false
		else
			print(hit.Name .. " is not a valid part")
		end
	end
end

btw im asking this question again cuz no one answered the first one

2 comments

0

Is there a particular reason for using raycasting? I like how you're doing something different from the traditional sword script!

0

i just heard that they are good for detecting collisions so thats why im using them