Is to many threads bad?

I made this server script in a car I have a lot of threads is that a bad thing? does it cause lag?
or is there something else wrong with my code? could I make it alot shorter? last question for the thread function is there a way I could do it better?

--Paths
local Humanoid = script.Parent.Humanoid0
local Front = script.Parent.Front
local Back = script.Parent.Back
local seat = Front.Parent.VehicleSeat
local SoundEvent = game.ReplicatedStorage:WaitForChild("Sound")
--Variables
local HIT_HUM_HP = 1/3
local OUR_HUM_HP = 1/5

local emitParticles = false

-- functions
local function spark(Part)
	if not Front.Parent.VehicleSeat.Crash.IsPlaying then
		Front.Parent.VehicleSeat.Crash:Play()
		task.delay(1.2,function()
			Front.Parent.VehicleSeat.Crash.Playing = false
		end)
	end
	
	for i, Particle in pairs(Part.Attachment:GetChildren()) do
		Particle:emit(50)
	end
end

local function Break()
	for i, v in pairs(seat.Parent.Union:getChildren()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = true
		end
	end
	warn("uh oh")
	if seat.Occupant then
		seat.Occupant.Parent:FindFirstChild("Drive"):Destroy()
		seat.Occupant.Health = 0
	end
	seat.Disabled = true
	seat.Died:Play()
	task.delay(4.243,function()
		seat.Gone:Play()
	end)
	
end

--thread funcions
local front = coroutine.create(function()
	Front.Touched:Connect(function(TouchedObject)
		print(TouchedObject)
		if TouchedObject.Parent:FindFirstChild("Humanoid") and TouchedObject.Parent ~= script.Parent and not game.Players:GetPlayerFromCharacter((TouchedObject or TouchedObject.Parent)) then
			TouchedObject.Parent.Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude*HIT_HUM_HP)
			if math.round(seat.AssemblyLinearVelocity.Magnitude) ~= 3 and seat.Occupant ~= nil then
				Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude*OUR_HUM_HP)
				if seat.Occupant then
					spark(Front, seat.Occupant.Parent)
				else
					spark(Front)
				end
			end
		elseif TouchedObject.Parent ~= script.Parent and not game.Players:GetPlayerFromCharacter((TouchedObject or TouchedObject.Parent)) then
			Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude*OUR_HUM_HP)
			if seat.Occupant and math.round(seat.AssemblyLinearVelocity.Magnitude) ~= 3 then
				spark(Front)
			else
				spark(Front)
			end
		end
	end)
end)

local back = coroutine.create(function()
	Back.Touched:Connect(function(TouchedObject)
		print(TouchedObject)
		if TouchedObject.Parent:FindFirstChild("Humanoid") and TouchedObject.Parent ~= script.Parent and not game.Players:GetPlayerFromCharacter((TouchedObject or TouchedObject.Parent)) then
			TouchedObject.Parent.Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude/4)
			print(Humanoid.Health)
			if math.round(seat.AssemblyLinearVelocity.Magnitude/6) ~= 0 and seat.Occupant ~= nil then
				Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude/6)
				if seat.Occupant then
					spark(Back, seat.Occupant.Parent)
				else
					spark(Back)
				end

			end
		elseif TouchedObject.Parent ~= script.Parent then
			Humanoid.Health -= math.round(seat.AssemblyLinearVelocity.Magnitude/1)
			if seat.Occupant and math.round(seat.AssemblyLinearVelocity.Magnitude) ~= 3 then
				spark(Back, seat.Occupant.Parent)
			elseif math.round(seat.AssemblyLinearVelocity.Magnitude) ~= 3 then
				spark(Back)
			end

		end
	end)
end)

local Health = coroutine.create(function()
	local cart = seat.Parent
	Humanoid.HealthChanged:connect(function(hp)
		if Humanoid.Health <= 35 and Humanoid.Health ~= 0 then
			for I, particles in pairs(Front:WaitForChild("Broken"):GetChildren()) do
				particles.Enabled = true
			end	
		elseif Humanoid.Health == 0 then
			for I, particles in pairs(Front:WaitForChild("Broken"):GetChildren()) do
				particles.Enabled = false
			end
			Break()
			
			cart.Union.CustomPhysicalProperties = PhysicalProperties.new(10, .4, 1)
			cart.WheelBR:Destroy()
			cart.WheelBL:Destroy()
			cart.WheelFL:Destroy()
			cart.WheelFR:Destroy()
			coroutine.yield(front)
			coroutine.yield(back)
		end
	end)
end)

local sounds = coroutine.create(function()
	SoundEvent.OnServerEvent:Connect(function(plr, sound, name, play)
		if play then
			sound[name]:Play()
			if name == ("drive" or "screech") then
				emitParticles = true
			elseif name ~= ("drive" or "screech") then
				emitParticles = false
			end
		else
			sound[name]:Stop()
		end

	end)
end)

local Particles = coroutine.create(function()
	local EmiterBL = script.Parent.WheelBL.Attachment.ParticleEmitter
	local EmiterBR = script.Parent.WheelBR.Attachment.ParticleEmitter
	
	seat.Parent.WheelBR.Touched:Connect(function(floor)
		if emitParticles and seat.AssemblyLinearVelocity.Magnitude > 5 and math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1)))) < 10 then
			EmiterBR.Color = ColorSequence.new(floor.Color)
			EmiterBR.Enabled = true
			print(math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1)))))
		else
			print(tostring(math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1))))).."WERE UPSIDE DOWN")

			EmiterBR.Enabled = false
		end
	end)
	seat.Parent.WheelBL.Touched:Connect(function(floor)
		if emitParticles and seat.AssemblyLinearVelocity.Magnitude > 5 and math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1)))) < 10 then
			EmiterBL.Color = ColorSequence.new(floor.Color)
			EmiterBL.Enabled = true
			print(math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1)))))
		else
			print(tostring(math.round(math.deg(math.acos(math.clamp(script.Parent.PrimaryPart.CFrame.UpVector:Dot(workspace.Baseplate.CFrame.UpVector),-1,1))))).."WERE UPSIDE DOWN")

			EmiterBL.Enabled = false
		end
	end)
end)

-- coroutines
coroutine.resume(sounds)
coroutine.resume(Health)
coroutine.resume(front)
coroutine.resume(back)
coroutine.resume(Particles)

sorry the script is soo long :woozy_face:

2 Likes

One issue that occurs constantly is when I crash and my speed is at 0 the particles keep emitting

1 Like

You’d need to use thousands of threads before it’s too much. In this case, you’re not even close to a hundred, so you’re OK. The source of lag would be the work being done in those threads.

However, you don’t need to use threads. There’s no need to connect to events asynchronously. Events when fired, don their thread. You’d only need to use them when you want to still do work without waiting. An example is in the Break function where you defer playing a sound effect for 4.243 seconds.

Also, it’s important to clarify that threads aren’t threads that run in parallel. They essentially share the total time to do their work.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.