Damaging a car with a gun

The vehicle has been damaged by attaching an int value to the vehicle. It works fine in the event of a collision, but it doesn’t get damaged in the event of a gun attack. I want to reduce the int value when attacking with a gun.

–lua scrpit–
local carhealth = script.Parent.Health.Value
script.Parent.Touched:Connect(function()
carhealth = carhealth -30

I put the script on the car parts, and I put it on the bullets, but it didn’t work.

2 Likes
script.Parent.Touched:Connect(function(otherPart)
    local carHealth = script.Parent.Health
    if otherPart.Parent = car then
        carHealth.Value = carHealth.Value - (30)
    end
end)

Perhaps make a script in the guns side.
'If the guns bullet hit something, search for the carHealth.
If carHealth exist, reduce the value to the desired amount.'

This stores the number value inside the variable, so when you change it the actual Health.Value won’t go down. So instead use:

local HealthValue = script.Parent:WaitForChild("Health")

Then when we are going to change it we use:

HealthValue.Value = HealthValue.Value - 30
HealthValue.Value -= 30

(both are the same)

Now you have to make sure whatever touches the car is the gun

script.Parent.Touched:Connect(function(hitPart)
   print(hitPart.Name)
end)

Use that script to get the name of what hits the gun then once you know it use:

local HealthValue = script.Parent:WaitForChild("Health")

script.Parent.Touched:Connect(function(hitPart)
   if hitPart.Name == "NAMEHERE" then
      HealthValue.Value -= 30
   end
end)
1 Like

I modified it by applying your method. Thank you so much!