I’m trying to make a sword that relies on raycasts rather than touch events because I find them inconsistent. It works by shooting a ray from the bottom of the blade to the tip, and anything caught in-between should take damage, although I haven’t implemented the damage quite yet, but that’s not the problem. The problem is that even when the sword goes through an enemy the raycast won’t detect anything. Here’s the script:
script.Parent.Attacking.Changed:Connect(function(newvar) ----i have a seperate script to tell what attack the playeer is making
if script.Parent.Attacking.Value == true then ------this way it wont do damage if it's not attacking
local HitYet = 1 -----Im going to use this later for single target and multi-target swings
local totaltime = 0 ----this will count up until it reaches the time it takes for the swing to stop, then it will end the loop
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Exclude ---i have a folder in the workspace full of enemies but I thought I was doing something wrong so I switched to Exclude
raycastparams.FilterDescendantsInstances = script.Parent.Parent:GetDescendants() ---do I use {}? does it change anything I didn't notice? (this is to ignore the player and the weapon)
local runserve = game:GetService("RunService") ---im defining runservice like this so I can break the loop later
local Stepped ----for breaking the loop
Stepped = runserve.Heartbeat:Connect(function(timenum, deltatime)
local ray = workspace:Raycast(script.Parent.Handle.Position, script.Parent.Tip.Position, raycastparams) ---a ray from the base to the tip
if ray and ray.Instance then
script.Parent.Attacking.Value = false ---stops it from doing more damage then it should
Stepped:Disconnect()
print(ray.Instance.Name)
end
totaltime += timenum ----counts up how long the swing has lasted
if totaltime >= script.Parent.AttackTime.Value then ---and if it's been too long the raycast loop ends
script.Parent.Attacking.Value = false
Stepped:Disconnect()
print(totaltime, " times up" )
end
end)
end
end)
----sorry about the strange indentation
I thought the issue was the tip’s position not getting updated, so I added this in the middle of it:
local Pstart = Instance.new("Part")
Pstart.Parent = workspace
Pstart.Anchored = true
Pstart.Size = Vector3.new(0.5,0.5,0.5)
Pstart.BrickColor = BrickColor.new("Lime green")
Pstart.Position = script.Parent.Handle.Position
Pstart.Transparency = 0.5
Pstart.CanCollide = false
local Pstop = Instance.new("Part")
Pstop.Parent = workspace
Pstop.Anchored = true
Pstop.Size = Vector3.new(0.5,0.5,0.5)
Pstop.BrickColor = BrickColor.new("Really red")
Pstop.Position = script.Parent.Tip.Position
Pstop.Transparency = 0.5
Pstop.CanCollide = false
Pstop.CanQuery = false
Pstart.CanQuery = false
it creates parts on the edges of the swing and it showed me that the position was updated regularly enough to not be the problem.
Now I’m not certain what the issue could be and I don’t use both raycasting and runservice very often so any help is appreciated. Thanks!
Edit: I attempted to raycast between the parts created to show the swing but nothing got better, also sometimes the sword will register a hit even though the blade hasn’t touched it yet or won’t touch it later. Maybe the ray is going in the wrong direction?
unless you didn’t help