Car should stall after random amount of time

I want to achieve that my car stalls at a random time and plays the car failing sound while making the engine sounds .Playing property to false and after the random time that the engine sounds .Playing property is set to true and the car failing sound stops playing.

I got it working but not really,
i modified the script so much that it literally now breaks studio.
Can you guys correct my code or help me achieve my goal?

here is the script:

local FailSound = script.Parent.Chassis.FloorPanel.FailStart
local SoundDirectory = script.Parent.Chassis.FloorPanel --where the 3 engine sounds are located
local part = script.Parent.Chassis.FloorPanel --the part used for decreasing velocity
local Eng = true

while true do
	for _, Sound in ipairs(SoundDirectory:GetChildren()) do
		if Sound.Name == "Sound" then --finding 3 engine sounds that have the same name
			print("Found the Sounds")

			local randomDelay = math.random(1, 6)
			Eng = false
			wait(randomDelay)
			Eng = true
			while Eng do
				FailSound.Playing = true --Cars engine stalled
				print("Car failing to start")
				Sound.Playing = false --the sounds in the car should stop playing (engine sounds)
				script.Parent.Parent.Car:SetAttribute("MaxSpeed", 0)--speed is 0 because engine stalled
			if not (part.Velocity == Vector3.new(0,0,0)) then
				
					part.Velocity = part.Velocity * 20 -- tried to make the car slower by decreasing its velocity
					else
					
				randomDelay = math.random(1, 6)
				wait(randomDelay)

				Eng = false
				Sound.Playing = true
				FailSound.Playing = false
				script.Parent.Parent.Car:SetAttribute("MaxSpeed", 101.5) -- after a random time, car finally starts and drives

				randomDelay = math.random(1, 6)
				wait(randomDelay)

				Eng = true
				
			end
		end
	end
	end
	end

I would be very happy to get this code working

1 Like