Issues with car crash script

Hello, so I’ve come up with a simple car crash script that is located inside of StarterCharacterScripts. The script works for its intended use, killing people if they’re hit by a car. However, the issue arises when people start jumping on each other, people can brutally murder other players by just jumping on them. I’ve added a check into the script that should filter out any players, yet it doesn’t seem to do anything. If you could help me it would be greatly appreciated!

Script:

local CollisionSpeed = 30

script.Parent.HumanoidRootPart.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent:FindFirstChild("HumanoidRootPart") then
		print("Humanoid detected")
	elseif Hit.Velocity.Magnitude > CollisionSpeed then
		script.Parent.Humanoid.Health =- 100
	end
end)

Rather than filtering out players, I think it might be better if you made it so that it only accepts vehicles, by using tags (Collection Service) to only inflict damage if it was hit by a car.

1 Like

Made some adjustments using your recommendations and come up with this script, have I utilized CollectionService properly?

Script:

local CollectionService = game:GetService("CollectionService")

local CollisionSpeed = 30

script.Parent.HumanoidRootPart.Touched:Connect(function(Hit)
	if CollectionService:HasTag(Hit.Parent.Parent, "Car") then
		if Hit.Velocity.Magnitude > CollisionSpeed then
			script.Parent.Humanoid.Health =- 100
		end
	end
end)

hey, i think ur approaching it the right way by using CS. im pretty sure u gotta use GetTagged tho

EDIT sorry mb its:

local CollectionService = game:GetService("CollectionService")

local CollisionSpeed = 30

script.Parent.HumanoidRootPart.Touched:Connect(function(Hit)
if not CollectionService:GetTagged(Hit.Parent.Parent, "Car") then return end
	if CollectionService:GetTagged(Hit.Parent.Parent, "Car") then
		if Hit.Velocity.Magnitude > CollisionSpeed then
			script.Parent.Humanoid.Health = -100
		end
	end
end)
1 Like

I think HasTag is correct, it’s listed on the documentation as the way you would check if a object has a tag.

For clarifcation: I’m only checking if a single instance has a tag, not multiple. I think your script would work if I were checking multiple. Thanks for the help though.

1 Like

oh yes ur correct. i havent used hastag b4. i was reading another script i had. this is a bit out of my depth but ur idea sounds brilliant. gl w figuring it out! :smiley:

1 Like