I need help applying damage to a value in a car and getting the UI to correspond to it

I’m sorry for the long post, I just wanted to include as much information as possible.

I have a car model that I want to deal damage to when it hits an object. I have each object set to deal a different amount of damage. In the output, it detects (usually) when the object is hit and says it will deal the damage, but it doesn’t actually do it. Here’s what I have set up:


This is the car with the damage value in the body, which is in the chassis.
I have a remote event in replicated storage to deal with the damage event.
Screenshot 2024-07-05 at 10.04.58 PM
In startergui, in a localscripts folder, I have a cardamagescript:

``local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DamageUpdateEvent = ReplicatedStorage:WaitForChild("DamageUpdate")

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local damageBar = playerGui:WaitForChild("Driver"):WaitForChild("Frame") -- Adjust path as necessary

-- Function to update the UI
local function updateDamageUI(damageValue)
    print("Updating UI with damage value: " .. damageValue)
    local damagePercent = damageValue / 100
    damageBar.Size = UDim2.new(damagePercent, 0, 1, 0)
    print("Damage bar size updated to: " .. damageBar.Size.X.Scale)
end

DamageUpdateEvent.OnClientEvent:Connect(updateDamageUI)

print("CarDamageScript loaded and running.")

In serverscriptservice, I have a damagehandler script:

print("DamageHandler script started")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DamageUpdateEvent = ReplicatedStorage:WaitForChild("DamageUpdate")

local obstaclesFolder = game.Workspace:WaitForChild("Obstacles1", 10)

if obstaclesFolder then
    print("Obstacles folder found: " .. obstaclesFolder.Name)
else
    warn("Obstacles folder not found")
end

local damageValues = {
    ["rock"] = 10,
    ["spike"] = 20,
    ["traffic cone"] = 5
}

local function applyDamageToCar(body, amount)
    local damageValue = body:FindFirstChild("Damage")
    if damageValue and damageValue:IsA("NumberValue") then
        damageValue.Value = math.clamp(damageValue.Value + amount, 0, 100) -- Ensure damage doesn't exceed 100
        print("New damage value after applying damage: " .. damageValue.Value)
        DamageUpdateEvent:FireAllClients(damageValue.Value)
    else
        print("Damage value not found or not a NumberValue in body: " .. body.Name)
    end
end

local function onTouched(hit)
    print("Touched event fired by: " .. hit.Name)

    local obstacleName = hit.Name:lower()
    local damageAmount = damageValues[obstacleName]
    if damageAmount then
        print("Obstacle detected: " .. obstacleName .. ", Damage amount: " .. damageAmount)
        
        local hitbox = hit.Parent
        if hitbox:IsA("Model") then
            local body = hitbox.Parent
            local chassis = body and body.Parent
            local car = chassis and chassis.Parent

            if car and car:IsA("Model") and chassis and chassis:IsA("Model") and body and body:IsA("Model") then
                print("Car detected: " .. car.Name)
                applyDamageToCar(body, damageAmount)
            else
                print("Chassis or car not found")
            end
        end
    else
        print("Non-obstacle part detected, no damage applied: " .. hit.Name)
    end
end

local function connectTouchedEvents()
    local carModel = game.Workspace:WaitForChild("Basic sedan", 10)

    if carModel then
        print("CarModel found: " .. carModel.Name)
    else
        warn("CarModel not found")
        return
    end

    local hitboxes = {"FrontHitbox", "RearHitbox", "RoofHitbox", "BottomHitbox"}

    for _, hitboxName in ipairs(hitboxes) do
        local hitbox = carModel:FindFirstChild(hitboxName, true)
        if hitbox then
            hitbox.Touched:Connect(function(hit)
                if damageValues[hit.Name:lower()] then
                    onTouched(hit)
                end
            end)
            print("Connected Touched event for hitbox: " .. hitboxName)
        else
            warn("Hitbox not found: " .. hitboxName)
        end
    end

    for _, obstacle in ipairs(obstaclesFolder:GetChildren()) do
        if damageValues[obstacle.Name:lower()] then
            obstacle.Touched:Connect(onTouched)
            print("Connected Touched event for obstacle: " .. obstacle.Name)
        end
    end
end

connectTouchedEvents()
print("DamageHandler script loaded and events connected.")

I believe that this part is the problem, as it is not printing in the output:

local function applyDamageToCar(body, amount)
    local damageValue = body:FindFirstChild("Damage")
    if damageValue and damageValue:IsA("NumberValue") then
        damageValue.Value = math.clamp(damageValue.Value + amount, 0, 100) -- Ensure damage doesn't exceed 100
        print("New damage value after applying damage: " .. damageValue.Value)
        DamageUpdateEvent:FireAllClients(damageValue.Value)
    else
        print("Damage value not found or not a NumberValue in body: " .. body.Name)
    end
end

Here’s what I have in the explorer:

And here is my output when I hit an object (I’d like to simplify the hitbox collision part as I don’t want it to detect it, but I don’t think that’s the problem):