Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[HOW TO?] PartA Moves Away From PartB

Using a BodyVelocity, I am trying to achieve a "knockback" type feel.

How would I go about getting repelling one part from another?

I basically want it so when a humanoid dies, they get flung away from the humanoid that killed them.

1 comment

0

*I already have the death script and everything, all I need now is to calculate a way to fling a part AWAY from another part.

1 answer

Just wrote a script for this to figure out how I would do it. To explain it, all I did was use Vector3.Unit and add a BodyForce using the value that provided me with. Vector3.Unit basically just returns a direction with a magnitude of 1 (if you didn't take physics or learn what a vector is then that will be confusing but I highly recommend researching vectors if you do want to get into scripting). Anyway, now that I had the direction I simply added a force into the player I hit that went away from where I was standing.

local tool = script.Parent
local handle = tool.Handle

local debounce = false
local cooldown = 1.5

tool.Activated:Connect(function()
	handle.Touched:Connect(function(hit)
		if debounce then return end
		debounce = true
		local plr = tool.Parent
		local h = hit.Parent:FindFirstChild("Humanoid")
		if h and h.Health > 0 then
			local hrp = hit.Parent.HumanoidRootPart
			
			local bf = Instance.new("BodyForce")
			bf.Parent = hrp
			local forceValue = (hrp.Position - plr.HumanoidRootPart.Position).Unit * 2500
			bf.Force = Vector3.new (forceValue.x, -20, forceValue.z)
-- mess around with the y value on the line above this a lot, I changed it to what I liked.
			h.Jump = true
			wait(0.3)
			bf:Destroy()
			wait(cooldown - 0.3)
			debounce = false
		end
	end)
end)

I'm assuming you can understand everything else in the script because you mentioned writing everything else just fine, but let me know if you want something else explained. Hope this helps!

EDIT: Forgot to mention that I didn't pay attention much when making the tool so once you click once it will constantly give other players knockback without you having to click.

This discussion is preserved as it was. New answers, comments, and votes are closed.