If this is about causing knockback in a player, or an NPC you would clone from storage, something I like to do in this case is to have a bodyforce in the primarypart of the player or NPC before it gets put into the workspace. That way, you just access the NPC or player's bodyForce and apply a force to it whenever you want.
So it seems as if you are attacking NPCs, so to have a script place a bodyVelocity in an NPC the script would be like:
workspace.ChildAdded:Connect(function(child)
if child.Name == "whatever the bodyforce is going to be in" then
local bv = Instance.new("BodyVelocity")
bv.Parent = child.PrimaryPart
end
end)
Your script would look like:
local player = game.Players.LocalPlayer
local handle = script.Parent.Handle
local debounce = false
local playerPos = player.PrimaryPart.CFrame
handle.Touched:Connect(function(hit)
if not debounce and hit and hit.Parent.Humanoid then
debounce = true
local hitPos = hit.Parent.PrimaryPart.CFrame
hit.Parent.Humanoid:TakeDamage(25)
hit.Parent.PrimaryPart.bodyForce.Force = (playerPos-hitPos).unit*-3
wait(.5)
hit.Parent.PrimaryPart.bodyForce.Force = Vector3.new(0,0,0)
wait(14.5)
debounce = false
end
end)
To explain the bodyForce line, it makes it so that Point A is you and Point B is the enemy, but it is multiplied by a negative number so that the enemy is knocked away from you while being in line with you.
To visualize this, you are the end of a stick, the enemy is the fulcrum (the point a lever rotates on) and if we multiplied the force by a positive number, the enemy would go towards you, if we multiply it by a negative number, the enemy goes to the opposite end of the stick.
3 comments
Insert a BodyForce in the player's torso learn more on the wiki
To get a direction vector from point A to point B, do
(B - A).unit * speed. So if you wanted to fling a brick from (1, 2, 3) towards (4, 5, 6) with a speed of 5, you'd do((4, 5, 6) - (1, 2, 3)).unit * 5.is the humanoid part of a player?