I’m trying to make a script that deals damage when hit by an object. I wrote a simple script using the touched
event with object.magnitude
.
humanoid.touched:Connect(function(hit)
local magnitude = hit.Velocity.Magnitude
if magnitude >= 40 then
humanoid:TakeDamage(magnitude/15)
end
end)
It seems to work fine at first, but after some playtesting, I found out a few problems. When the player is standing on an moving object (for example a car), the player takes damage even when nothing is hitting them. Also, when the player is being flinged towards a wall, he takes no damage because the script only checks for moving objects. I’ve tried adding the velocity of the HumanoidRootPart
to magnitude
, then tweak the numbers, but now the player takes damage when starting to jump. Now, I’m stuck here and I have no idea what to do
Is there any way to fix this problem, or is there a more reliable way to deal damage to the player when hitting objects?