Hello! I’m currently trying to make a gun that ricochets in my project, it works as following:
It first does a raycast to where the mouse is pointing, simple stuff. When it hits something, it’ll reflect repeating the same process 3 times.
Problem is, if you aim infront of a character’s feet, it’s supposed to ignore the character(due to it being in raycastparameters exclude list) and just reflect the raycast, however, it reflects all 3 times, dealing more damage than supposed to. (I’m sure some people will exploit this glitch if I leave it in)
On top of that, sometimes when repeating the ricochet effect for the first/second time and on a closed area, it’ll go through walls instead of ricocheting off them.
How could I go about fixing those issues?
Here’s the “reflect repeating the same process 3 times.” part of my script, which is where I believe the problem is originating from.
local Direction
local Reflect
if Raycast then
Direction = (Hit - script.Parent.Parent.BarrelTemplate.Position).Unit
Reflect = (Direction - (2 * Direction:Dot(Raycast.Normal) * Raycast.Normal))
end
local counter = 0
for i = 1, Values.RedRicochetCounter.Value do
if Raycast and Raycast.Normal then
local FilterTable = {workspace.Map.Regions}
for i, character in pairs(workspace:GetChildren()) do
if character:IsA("Model") and character:FindFirstChild("Humanoid") then
table.insert(FilterTable, character)
end
end
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = FilterTable
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastParams2 = RaycastParams.new()
raycastParams2.FilterDescendantsInstances = {workspace.Map.Regions}
raycastParams2.FilterType = Enum.RaycastFilterType.Exclude
local RicochetRaycast = workspace:Raycast(Raycast.Position, Reflect * 500, raycastParams)
local RicochetRaycast2 = workspace:Raycast(Raycast.Position, Reflect * 500, raycastParams2)
if RicochetRaycast2 then
if RicochetRaycast2.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid") then
local Humanoid = RicochetRaycast2.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChild("Humanoid")
if Humanoid.Health and Humanoid.Parent:FindFirstChild("CanDie") then
if Humanoid.Parent.CanDie.Value == true then
Humanoid:TakeDamage(Values.Damages.RedDamage.Value)
print("also took damage")
end
end
end
if RicochetRaycast then
Raycast = RicochetRaycast
Direction = Reflect
end
counter = counter + 1
end
end
end
Script is a serverside, plays after being called by a remote event from the gun’s local script.
The “Raycast.Normal” raycast seen in the top of the script is the raycast that has it’s direction the same as the firing player’s mouse.
CanDie is a value in the character.
I’m sorry that I sent a long part of my script, that is due to me not wanting to have issues with not sending enough information.