How can I recreate this effect?

I am working on creating this thing where if a certain part hits you with enough force or speed, you get knocked flying back and after like 1 second, you die.

Here’s a ROBLOX game I played and recorded as an example

In my case the damage you receive depends on the speed of the train, if the train is going relatively slow it’ll knock you back and inflict a bit of damage on you, if the train is at a higher speed, you get knocked back father with a higher damage inflict, any faster you’ll get knocked back and die (reset character) after a second.

The thing that hits you is a mesh part in my train called “COWCATCHER”. Here’s the hierarchy.

I’m willing to write the code for it, I just don’t know how I’ll be able to do that so I’m looking here to answers

Thanks :slight_smile:

One of the easiest way is to create a hitbox in front of the train that detects humanoids and damage/knock them back according to the current velocity of that hitbox.

From the video, the knockback is just the byproduct of the character getting flung from the contact, and the respawn is just the default spawning behavior. However, you can override it by calling LoadCharacter on the Player.

2 Likes

If you don’t mind me asking, how exactly can I get the velocity of the hitbox?

1 Like

Looks like a standard object with collection hitting an object with collection at speed to me with a bounce back script added …

This is a bounce back script, you could also add damage based off speed. (set up to test, atm)

--serverscript .. atm in a part (with touch enabled) on the workspace

local db=true
script.Parent.Touched:Connect(function(hit)
	local humanoid=hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid and db then	db=false
		local bv=Instance.new("BodyVelocity")
		bv.Parent=hit.Parent:WaitForChild("LowerTorso")
		bv.MaxForce=Vector3.new(100000, 100000, 100000)
		bv.Velocity=hit.Parent.Head.CFrame.LookVector * -80 
			+ hit.Parent.Head.CFrame.UpVector * 80
		wait(0.01)bv:Destroy()db=true
		wait(1)
	end
end)

This correctly calculates the bounce-back direction based off the player’s facing direction.

2 Likes

You can use BasePart.AssemblyLinearVelocity, assuming your hitbox is welded and is moving via physics (not CFrame).

2 Likes

What if, hypothetically said hitbox is moving via CFrame?

Assuming it’s anchored, physics doesn’t apply in these situations (which is why I avoid CFrame based manipulation).

You can probably get away by keeping track of it’s velocity magnitude by comparing it’s distance every heartbeat (you can rate limit if you want to) and running spatial queries.

1 Like

Okay so I modified said script you made and got this

local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local Hit_Character = hit.Parent:FindFirstChild("Humanoid").Parent
		local bv = Instance.new("BodyVelocity")
		local offset = Vector3.new()
		bv.Parent = Hit_Character.Torso or Hit_Character.UpperTorso
		bv.MaxForce = Vector3.new(100000, 100000, 100000)

		local partVelocity = part.Velocity.Magnitude
		local direction = part.CFrame.LookVector  

		if partVelocity < 40 then
			bv.Velocity = (direction * -20) + (Vector3.new(0, 20, 0)) 
			Hit_Character:FindFirstChild("Humanoid"):TakeDamage(10)
		elseif partVelocity < 70 then
			bv.Velocity = (direction * -40) + (Vector3.new(0, 40, 0))
			Hit_Character:FindFirstChild("Humanoid"):TakeDamage(30)
		else
			bv.Velocity = (direction * -80) + (Vector3.new(0, 80, 0)) 
			wait(0.28)
			Hit_Character:FindFirstChild("Humanoid").Health = 0
		end

		wait(0.01)
		bv:Destroy()
	end
end)

I got this mess lol

[Google Drive Link, sorry the file was too big for me to upload here]

Only problem though, I was testing this out and I was hoping it would knock the players (in this case the zombies) back in the direction it was facing, not up in the sky. Any ideas on how I can fix this?

looks like in this one the direction is determined by:
local direction = part.CFrame.LookVector
(the direction that part is facing)

Oh, then how come in the video theyre getting thrown up instead of straight ahaead? the part isnt facing up?

Try creating a transparent block and welding it to the cow catcher, sticking out a bit, with the script inside it. … angle the block so it’s face (forward) is at an angle you like for the bounce back.
(you’ll have to play around with it to find your sweet spot and bounce back settings)

I’m glad you got it working. The script in the cow catcher made it so you were not free to move the part in any angle you wished. I also use a decal in the transparent part to show me what way is forwards. After I get it all working I remove the decal.

I noticed this one is using the part itself to get the direction. For the life of me I can’t find the version where I made it go off the player to get the direction by reversing the players facing direction (it also used LookVector but from the player). That one worked well as no matter the angle you hit the part it would always bounce the player straight back and up a bit. I’ll have to go re-wright that as I thought this was that one.

I see you used: = Hit_Character.Torso or Hit_Character.UpperTorso
Amazingly easy way to keep things compatible.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.