I would like to make a knockback system where a player gets launched back while playing an animation. How should I do this? I’ve never made anything like this before and I really need help. I’m not asking for a script, I am just asking how I should execute this.
local part = script.Parent
function onTouch(hit)
if hit.Parent == nil then return end
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
print"player"
local player = game:GetService("Players")[hit.Parent.Name]
local bv = Instance.new("BodyVelocity", player)
local hasBV = false
if hasBV == false then
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.P = 1000
bv.Velocity = Vector3.new(5, 0, 0)
hasBV = true
end
end
end
part.Touched:Connect(onTouch)
It doesn’t seem to work though. Did I use BodyVelocity in a wrong way? I’ve never used it before.
function onTouch(hit)
if hit.Parent == nil then return end
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
print"player"
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) -- Here Is the problem maybe
local bv = Instance.new("BodyVelocity", player)
local hasBV = false
if hasBV == false then
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.P = 1000
bv.Velocity = Vector3.new(5, 0, 0)
hasBV = true
end
end
end
part.Touched:Connect(onTouch)
Now, that I’ve gotten the BodyVelocity to work, how do you get the character to stop flying back?
local part = script.Parent
function onTouch(hit)
if hit.Parent == nil then return end
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
print"player"
local player = hit.Parent.HumanoidRootPart
if not hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
local bv = Instance.new("BodyVelocity", player)
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.P = 1000
bv.Velocity = Vector3.new(-100, 0, 0)
end
end
end
part.Touched:Connect(onTouch)
local part = script.Parent
function onTouch(hit)
if hit.Parent == nil then return end
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
print"player"
local player = hit.Parent.HumanoidRootPart
if not hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
local bv = Instance.new("BodyVelocity", player)
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.P = 1000
bv.Velocity = Vector3.new(-100, 0, 0)
end
wait(0.25)
bv:Destroy()
end
end
part.Touched:Connect(onTouch)
You can make it using linear velocity and an attachment. I think you can do it with something like this:
local part = script.Parent
function KnockBackOnHit(hit)
if hit.Parent == nil then return end
local h = hit.Parent:FindFirstChild("Humanoid")
local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")
if h ~= nil then
local att = Instance.new("Attachment", hrp)
local kbvel = Instance.new("LinearVelocity", att)
local hasLV = false
if hasLV == false then
kbvel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
kbvel.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
kbvel.Attachment0 = att
kbvel.VectorVelocity = hrp.Position - part.Position * Vector3.new(5, 0, 0)
game.Debris:AddItem(att, 0.4)
end
end
If it were for a player, it’d be that player’s humanoidrootpart position minus your humanoidrootpart position. It was just a simple way to do knockback