Hi guys, I made a custom knockback system for my game, and for some reason, the knockback feels off, maybe due to the knockback not directing appropriately.
Here’s the script. It is made up of 2 separate scripts, one for the server side and the other receives the RemoteEvent.
Just ask if you guys need clarification or more info.
-- Server Script
module.StunGrenade = function(Position : Vector3)
task.spawn(function()
task.spawn(function()
local Attachment = Instance.new("Attachment")
Attachment.Parent = workspace.Baseplate
Attachment.Name = "ParticleAttachment"
Attachment.WorldPosition = Position
local LongestLife = 0
local Blast = Particles.Blast
for i, v : ParticleEmitter in pairs(Blast:GetChildren()) do
if v:IsA("ParticleEmitter") then
local Particle = v:Clone()
Particle.Parent = Attachment
Particle:Emit(Particle:GetAttribute("EmitCount"))
if Particle.Lifetime.Max > LongestLife then
LongestLife = Particle.Lifetime.Max
end
end
end
task.wait(LongestLife)
Attachment:Destroy()
end)
local HitCharacters = {}
local HitCharacterParts = {}
local Trajectory = {}
local Hit = workspace:GetPartBoundsInRadius(Position, 15, OverFilter)
for i, v in pairs(Hit) do
if v.Parent:IsA("Model") or v.Parent == workspace then
local InitialForce = 150
local Negate = (Position - v.Position)
local Distance = Negate.Magnitude
local Direction = Negate.Unit
--print(Direction)
local Force = -Direction * (InitialForce * (1 - (Distance / 15)))
if v.Parent:IsA("Model") then
if table.find(HitCharacterParts, v) or not v then
continue
end
table.insert(HitCharacters, v)
local player : Player
if Players:GetPlayerFromCharacter(v.Parent) then
player = Players:GetPlayerFromCharacter(v.Parent)
end
table.insert(HitCharacters, v.Parent)
Trajectory[v.Parent] = Force
if player then
KnockbackEvent:FireClient(player, Trajectory[v.Parent], v.Name)
else
if not v or not v.Parent or not v.Parent.PrimaryPart then
continue
end
v.Parent.PrimaryPart:ApplyImpulse(Trajectory[v.Parent])
end
else
v:ApplyImpulse(Force * v.Mass)
end
end
end
HitCharacters = nil
Hit = nil
Trajectory = nil
end)
end
-- Local Script
KnockbackEvent.OnClientEvent:Connect(function(Trajectory : Vector3, partName : string)
player.Character[partName]:ApplyImpulse(Trajectory)
end)