Hello. I was making a experimental game. When I tried to script a car damage script, it wont work.
Script:
local body = script.Parent
function crashed(hit)
for _, parts in pairs(body:GetDescendants()) do
parts.Health.Value = 100
if parts:IsA("BasePart") then
if parts.Velocity.Magnitude > hit.Velocity.Magnitude + 45 and hit.CanCollide == true or hit.Velocity.Magnitude > script.Parent.Velocity.Magnitude + 35 then
script.Parent.Health.Value = script.Parent.Health.Value - script.Parent.Velocity.Magnitude / 1.25
if parts.Health.Value == 0 then
for _, welds in pairs(body:GetDescendants()) do
if welds:IsA("Weld") then
welds:Destroy()
elseif welds:IsA("WeldConstraint") then
welds:Destroy()
end
end
end
end
end
end
end
Hello! I’d be happy to help you debug your car damage script.
Firstly, it seems like the “crashed” function is missing an event connection to an appropriate event. For example, you might want to connect this function to the “Part” or “Touch” events of your car so that the function is called whenever a part of the car collides with something.
Secondly, it looks like the logic within the “crashed” function might need some refinement. Specifically, the following line may not be doing what you intend:
if parts.Velocity.Magnitude > hit.Velocity.Magnitude + 45 and hit.CanCollide == true or hit.Velocity.Magnitude > script.Parent.Velocity.Magnitude + 35 then
Here, you are checking whether the velocity magnitude of a part of the car is greater than the velocity magnitude of the colliding object plus 45, AND whether the colliding object can collide with the car, OR whether the velocity magnitude of the colliding object is greater than the car’s velocity magnitude plus 35. However, it’s not clear what the intended behavior is when these conditions are met. Depending on what you want to happen, you may need to modify this line or add more logic to the function.
Finally, it’s worth noting that the script is currently setting all of the parts’ “Health” values to 100 at the beginning of the “crashed” function. Depending on the intended behavior of your game, you may want to adjust this to reflect the current health of the car before the collision.
I hope this helps! Let me know if you have any further questions.