Car Damage System Help

local part = script.Parent
local plr = game.Players.LocalPlayer
local driverseat = script.Parent.Parent.Parent:FindFirstChild("DriveSeat")

local function findPlayerCharacter()
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Character and player.Character:FindFirstChildOfClass("Humanoid") then
            return player.Character
        end
    end
end

local function findPlayerHumanoid()
    local playerCharacter = findPlayerCharacter()
    if playerCharacter then
        return playerCharacter:FindFirstChildOfClass("Humanoid")
    end
end

local function carhit(otherPart)
    -- Check if the player's humanoid is in the workspace and the player is seated in the driver seat
    local humanoid = findPlayerHumanoid()
    if humanoid and driverseat.Occupant == humanoid.Parent and otherPart == driverseat then
        -- Make it so when the player hits a wall at a velocity enough to damage the player inside the driver seat, damage is based on the speed of the car
        local speed = driverseat.Velocity.Magnitude
        if speed > 50 then
            humanoid:TakeDamage(speed / 10)
        end
    end
end

part.Touched:Connect(carhit)

What I’m trying to accomplish is: get the character of the player in the driverseat and damage their humanoid if they hit a wall at enough speed to damage them like in the script. nothing appears in the output

1 Like

What you probably need to do is Weld an invisible brick in front of the car, and connect a Touched function to it. Once the Touched function makes contact with a wall, check if the car speed is over the threshold then make the player take damage.

1 Like

Yeah that’s what the scripts already done but it just doesnt wanna work at all, as OP ive already made it try to functon…

1 Like

what part of the car is ‘part’?

a collision invisible anchored collission on part that is inside the car body model.

Is the invisible brick unAnchored and CanCollide is turned on?

It is anchored.

/////////////////////////////

1 Like

unAnchored the part…

1 Like

No because then the part isnt in the car / wont move with the car…

1 Like

If the part is anchored and welded at the same time. Ofcourse it won’t move

1 Like

It is not welded, the car moves, it just does not damage the driver when the car hits a wall…

1 Like

" This event only fires as a result of physics movement, so it will not fire if the CFrame property was changed such that the part overlaps another part. This also means that at least one of the parts involved must not be BasePart.Anchored at the time of the collision."

Could you possibly help me implement this.

You can’t use .Touched on anchored objects. You need to either unanchor the parts in the car, or find another way to check collisions using for example raycasting (this is probably too difficult for you, judging by what you’re stuck on)

Yeah, it’s difficult, I’m just trying to make it damage the player when the player hits a wall with the car, not damaging another player if they hit another player, but damaging the driver’s humanoid.

To do that, you’re best off unanchoring the frame of the car, and welding it to the driverseat. That way it’ll call .Touched

Wait so the part?

////////////

Yes, the part. The part which is representing the car’s body, should be unanchored. Weld it to the driverseat so that it stays in place.

Yep, doesnt work, I guess ill just give up on it lol.

local part = script.Parent
local driverseat = script.Parent.Parent.Parent:FindFirstChild("DriveSeat")

local function getOccupantHumanoid()
    local occupant = driverseat.Occupant
    if occupant then
        local character = occupant.Parent
        if character then
            return character:FindFirstChildOfClass("Humanoid")
        end
    end
end

local function carhit(otherPart)
    local humanoid = getOccupantHumanoid()
    if humanoid then
        local speed = driverseat.Velocity.Magnitude
        if speed > 50 then
            humanoid:TakeDamage(speed / 10)
        end
    end
end

part.Touched:Connect(carhit)