Make Car do damage when touched

So I’m trying to make an auto-driving car, and when you touch it you get damaged and flung backward
How can I make it do damage without it insta killing me?
Everything I’ve tried so far didn’t work


script.Parent.Car.Touched:Connect(function(hit)
	
	
	
	if hit.Parent:FindFirstChild("Humanoid") then
		
		local char = hit.Parent
	local fling = Instance.new("BodyVelocity")
		fling.MaxForce = Vector3.new(1,0,1) * 100000
		fling.Velocity = char.HumanoidRootPart.CFrame.lookVector * -100
		fling.Parent = char.HumanoidRootPart
	
	for count = 1, 10 do
		wait(0.1)
			fling.Velocity*= 0.7
			
			fling:Destroy()
			
			end
			
		
	end
end)

Can you not just use Humanoid.Health?

Just reduce the health when the player hits the car. (and when it hits 0 the player will die).

https://developer.roblox.com/en-us/api-reference/property/Humanoid/Health

You can also use the Humanoid:TakeDamage

https://developer.roblox.com/en-us/api-reference/function/Humanoid/TakeDamage

I tried that but it loops the script and kills the players instantly

Can you not just run it before the script loops?

Or if you mean it will contiue to kill due to the player still touching you could always use a cooldown or somthing.

I do not recommend using .Touched event

Try to use this module:

You can try to use a debounce like:

local Debounce = {}

script.Parent.Car.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not Debounce[hit.Parent] == true then
		local char = hit.Parent
		Debounce[char] = true
		
		local fling = Instance.new("BodyVelocity")
		fling.MaxForce = Vector3.new(1,0,1) * 100000
		fling.Velocity = char.HumanoidRootPart.CFrame.lookVector * -100
		fling.Parent = char.HumanoidRootPart

		for count = 1, 10 do
			wait(0.1)
			fling.Velocity*= 0.7
			fling:Destroy()
		end
		
		delay(2, function()
			Debounce[char] = nil
		end)
		
	end
end)
1 Like
local players = game:GetService("Players")

local parent = script.Parent
local car = parent.Car

local debounce = false

car.Touched:Connect(function(hit)
	if debounce then
		return
	end
	
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer then
			local hitHuman = hitModel:FindFirstAncestorOfClass("Humanoid")
			local hitHrp = hitModel:FindFirstChild("HumanoidRootPart")
			if hitHuman and hitHrp then
				debounce = true
				local BV = Instance.new("BodyVelocity")
				BV.MaxForce = Vector3.new(100000, 0, 100000)
				BV.Velocity = hitHrp.CFrame.LookVector *- 100
				BV.Parent = hitHrp
				
				for count = 1, 10 do
					task.wait(0.1)
					BV.Velocity *= 0.7
				end
				
				BV:Destroy()
				debounce = false
			end
		end
	end
end)

Shouldn’t the “BodyVelocity” instance be destroyed outside of the loop? Otherwise what would be the point of the increasing the body velocity’s “Velocity” property? I’ve also made some other minor optimisations/fixes, if you need any explained let me know.

1 Like