It does the necessary things and is as optimized as it can be.
It handles the collisions and physics interactions for every car in the game. About 30 cars are in game at times and it needs to handle them all at once.
Should I be concerned?
Is roblox going to improve it’s physics handling in that regard?
Here is the entire script.
local CollectionService = game:GetService("CollectionService")
local Module = require(game.Workspace["Game - Utilities"].DamageIndexes.Materials)
local function RecieveDamage(Hit, Child, Car)
if Hit and Child and Car then
if game.Workspace["Game - Utilities"]["Game - Script [CryptexRBLX]"].Timer.Value < 36 then return end
if Hit.Name == "Water" then
local water = Hit
if Child:FindFirstChild("Fire1") then
Child:FindFirstChild("Fire1"):Destroy()
end
if string.match(Child.Name, "Engine") or string.match(Child.Name, "Battery") or Child:IsA("VehicleSeat") then
print("is drowning")
wait(5)
while table.find(Child:GetTouchingParts(),water) do
wait(.04)
Child.Health.Value = Child.Health.Value - (Child.Health.Value * 0.04)
end
end
end
if ((Hit:IsDescendantOf(Car)) or (Hit.Locked == true or Hit.CanCollide == false)) and ((Child.Velocity - Hit.Velocity).Magnitude < 1000) then return end
if ((Child.Velocity - Hit.Velocity).Magnitude > 25 and not (Hit.Parent:FindFirstChild("Humanoid") or Hit.Parent.Parent:FindFirstChild("Humanoid"))) then
if not (Hit:IsA("MeshPart") or Hit:IsA("Accessory") or (Hit:IsA("Part")and Hit.Parent:FindFirstChild("Humanoid"))) then
if Hit.Name == "RepairBox" then
for i,v in pairs(Car:GetDescendants()) do
if v.Name == "Health" then
v.Value = (v.Parent:GetMass() * table.find(Module, v.Material))
end
end
end
local CIndex = table.find(Module, Child.Material)
local HIndex = table.find(Module, Hit.Material)
local Damage
if (CIndex > HIndex) then
Damage = ((((Child.Velocity - Hit.Velocity).Magnitude * math.abs(Child:GetMass() - Hit:GetMass()))/(CIndex-HIndex))*0.006)
elseif (CIndex < HIndex) then
Damage = ((((Child.Velocity - Hit.Velocity).Magnitude * math.abs(Child:GetMass() - Hit:GetMass()))/(HIndex-CIndex))*0.006)
elseif (CIndex == HIndex) then
Damage = (((Child.Velocity - Hit.Velocity).Magnitude * math.abs(Child:GetMass() - Hit:GetMass())) * 0.006)
end
if (Child.Name == "Armor") and Car:FindFirstChild("VehicleSeat") then
Damage = (Damage/(5+(Car.VehicleSeat.Configuration.ArmorStrength.Value*2.5)))
end
if (Child:FindFirstChild("Health")) then
Damage = math.abs(Damage)
Child.Health.Value = (Child.Health.Value - Damage)
if game.ReplicatedStorage[game.Players[tostring(Car.Owner.Value)].UserId]:FindFirstChild("Reward") then
game.ReplicatedStorage[game.Players[tostring(Car.Owner.Value)].UserId].Reward.Germophobic.Value = false
end
--print(Child.Name.. " has taken " ..Damage.. " damage from " ..Hit.Name.. " which belonged to " ..Hit.Parent.Name)
--print(Child.Name.. " now has "..Child.Health.Value.. " health")
if (Child.Health.Value < 0) then
local check = Child:FindFirstChild("Health")
if check then
check.Value = 0
--[[if Child.Name == "Chassis" then
local FindKiller = Hit:FindFirstAncestorWhichIsA("Model")
if FindKiller then
if FindKiller:FindFirstChild("VehicleSeat") then
local Killer = FindKiller:FindFirstChild("VehicleSeat").Occupant.Parent
if Killer then
if game.ReplicatedStorage[game.Players[Killer.Name].UserId]:FindFirstChild("Reward") then
game.ReplicatedStorage[game.Players[Killer.Name].UserId]:FindFirstChild("Reward").Demolisher.Value = true
end
end
end
end
end]]
local findfire = Child:FindFirstChild("Fire1")
if findfire then
findfire:Destroy()
findfire = nil
end
end
end
end
end
end
end
end
local function hasLights(Car)
for _, p in pairs(Car:GetDescendants()) do
if p:IsA("SpotLight") then
--print("we found spotlights")
return true
end
end
return
end
local function CarCheck(Car)
for i,v in pairs(Car:GetDescendants()) do
if v:IsA("BasePart") then
if v.Name == "Chassis" then
local chassisconnections = v:GetConnectedParts(true)
for i,weldedparts in pairs(chassisconnections) do
if chassisconnections == nil or #chassisconnections == 1 then
v.Parent.VehicleSeat.Health.Value = 0
print("vehicle seat has been disabled")
else
local carparts = Car:GetDescendants() do
for i,pieces in pairs(carparts) do
if pieces:IsA("BasePart") then
local obj = chassisconnections[table.find(chassisconnections,pieces)]
if obj then
-- print("object secured")
else
-- print("Object loose...")
if string.match("engine",string.lower(v.Name)) then
for i,v in pairs(v:GetDescendants()) do
if v:IsA("Sound") then
coroutine.wrap(function()
while v.Volume > 0 do
wait(0.2)
v.Volume = v.Volume - 0.01
v.PlaybackSpeed = v.PlaybackSpeed - 0.1
end
end)()
end
end
end
pieces.Parent = script.Map.Value
end
end
end
end
end
-- find the parts that are not connected to the chassis and delete them
end
end
end
end
end
local function CanChangeMovement(Car)
for _, p in pairs(Car.VehicleSeat:GetChildren()) do
if p.Name == "Translate" or p.Name == "Deploy" then
return true
end
end
end
local function CanBrake(Car)
for _, p in pairs(Car.VehicleSeat:GetChildren()) do
if p.Name == "Brakes" then
return true
end
end
end
--[[function TurnOffEngine(Car)
for i,v in pairs(Car:GetDescendants()) do
if v:IsA("Sound") then
while v.Volume > 0 do
wait(0.2)
v.Volume = v.Volume - 0.01
v.PlaybackSpeed = v.PlaybackSpeed - 0.1
end
end
end
end--]]
CollectionService:GetInstanceAddedSignal('Cars'):Connect(function(Car)
local VSConnection
local VS = Car:FindFirstChildWhichIsA("VehicleSeat")
if VS then
VSConnection = Car.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
--print("occupant changed seat whatever")
if Car.VehicleSeat.Occupant == nil then
Car.VehicleSeat.Throttle = 0
else
if hasLights(Car) then
game.ReplicatedStorage.LightEvent:FireClient(game:GetService("Players"):GetPlayerFromCharacter(Car.VehicleSeat.Occupant.Parent), Car)
end
if CanChangeMovement(Car) then
print(Car.Name.." can change movement")
game.ReplicatedStorage.ChangeMovement:FireClient(game:GetService("Players"):GetPlayerFromCharacter(Car.VehicleSeat.Occupant.Parent), Car)
end
if CanBrake(Car) then
game.ReplicatedStorage.Brakes:FireClient(game:GetService("Players"):GetPlayerFromCharacter(Car.VehicleSeat.Occupant.Parent), Car)
end
end
end)
end
local TotalCarHealth = 0
for _, Part in pairs(Car:GetDescendants()) do
if (Part:IsA("BasePart")) then
local Health = Instance.new("NumberValue",Part)
Health.Name = "Health"
Health.Value = (Part:GetMass() * table.find(Module, Part.Material))
--print(Health.Parent.Name.." now has " ..Health.Value.. "Health")
TotalCarHealth = TotalCarHealth + Health.Value
local te = nil
te = Part.Touched:Connect(function(Hit)
RecieveDamage(Hit, Part, Car)
if Part.Health.Value <= 0 then
te:Disconnect()
end
local CurrentCarHealth = 0
for i,v in pairs(Car:GetDescendants()) do
if v:IsA("NumberValue") and v.Name == "Health" then
CurrentCarHealth = CurrentCarHealth + v.Value
end
end
if CurrentCarHealth <= TotalCarHealth * 0.5 then
game.ServerScriptService.Locker.Scripts[game.Players[tostring(Car.Owner.Value)].UserId].Locked.Value = false
if game.ReplicatedStorage[game.Players[tostring(Car.Owner.Value)].UserId]:FindFirstChild("Reward") then
game.ReplicatedStorage[game.Players[tostring(Car.Owner.Value)].UserId].Reward.Survivor.Value = true
end
game.ServerScriptService.Locker.Scripts[game.Players[tostring(Car.Owner.Value)].UserId].Locked.Value = true
end
end)
Health = nil
end
end
for _, Part in pairs(Car:GetDescendants()) do
if (Part:FindFirstChild("Health")) then
local we = nil
we = Part.Health.Changed:Connect(function(Damage)
-- play sound
local OrigPartHealth
if OrigPartHealth == nil then
OrigPartHealth = Part.Health.Value + Damage
end
if Part.Health.Value <= OrigPartHealth * 0.25 then
for i,v in pairs(Part:GetChildren()) do
if string.match(string.lower(v.Name),"crack") then
v:Destroy()
v = nil
end
end
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Front"
Crack4 = nil
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Back"
Crack4 = nil
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Bottom"
Crack4 = nil
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Left"
Crack4 = nil
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Right"
Crack4 = nil
local Crack4 = game.ServerStorage.ParticleEffects.Cracks.Crack4:Clone()
Crack4.Parent = Part
Crack4.Face = "Top"
Crack4 = nil
end
if Part.Health.Value > OrigPartHealth * 0.25 and Part.Health.Value <= OrigPartHealth * 0.5 then
for i,v in pairs(Part:GetChildren()) do
if string.match(string.lower(v.Name),"crack") then
v:Destroy()
v = nil
end
end
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Front"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Back"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Bottom"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Left"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Right"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack3:Clone()
Crack.Parent = Part
Crack.Face = "Top"
Crack = nil
end
if Part.Health.Value > OrigPartHealth * 0.5 and Part.Health.Value <= OrigPartHealth * 0.75 then
for i,v in pairs(Part:GetChildren()) do
if string.match(string.lower(v.Name),"crack") then
v:Destroy()
v = nil
end
end
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Front"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Back"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Bottom"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Left"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Right"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack2:Clone()
Crack.Parent = Part
Crack.Face = "Top"
Crack = nil
end
if Part.Health.Value > OrigPartHealth * 0.75 and Part.Health.Value <= OrigPartHealth * 0.9 then
for i,v in pairs(Part:GetChildren()) do
if string.match(string.lower(v.Name),"crack") then
v:Destroy()
v = nil
end
end
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Front"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Back"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Bottom"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Left"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Right"
Crack = nil
local Crack = game.ServerStorage.ParticleEffects.Cracks.Crack1:Clone()
Crack.Parent = Part
Crack.Face = "Top"
Crack = nil
end
local Material = Part.Material
if not Part:FindFirstChildWhichIsA("ParticleEmitter") then
for i,v in pairs(game.ServerStorage.ParticleEffects.Sparks:GetChildren()) do
if v:IsA("BasePart") then
if v.Material == Part.Material then
for i,sparks in pairs(v:GetChildren()) do
coroutine.wrap(function()
local s = sparks:Clone()
s.Parent = Part
local colorKeypoints = {
-- API: ColorSequenceKeypoint.new(time, color)
ColorSequenceKeypoint.new( 0, Part.Color), -- At t=0, White
ColorSequenceKeypoint.new( 1, Part.Color) -- At t=1, Red
}
s.Color = ColorSequence.new(colorKeypoints)
-- play sound here
wait(1)
s.Enabled = false
wait(2)
s:Destroy()
s = nil
end)()
end
end
end
end
end
local pow = math.abs(Damage)
-- print(pow)
if string.match("engine",string.lower(Part.Name)) then
if Part.Health.Value < (Part:GetMass() * table.find(Module, Part.Material)) * .65 then
if not Part:FindFirstChildWhichIsA("Smoke") then
local Effect = Instance.new("Smoke")
Effect.Color = Color3.fromHSV(0,0,((Part.Health.Value/(Part:GetMass() * table.find(Module, Part.Material)))))
Effect.Parent = Part
Effect = nil
else
Part:FindFirstChildWhichIsA("Smoke").Color = Color3.fromHSV(0,0,((Part.Health.Value/(Part:GetMass() * table.find(Module, Part.Material))))*255)
end
end
elseif Part.Name == "Fueltank" or string.match("engine",string.lower(Part.Name)) then
if (Part.Health.Value <= (Part:GetMass() * table.find(Module, Part.Material)) * .5) and (Part.Health.Value > (Part:GetMass() * table.find(Module, Part.Material)) * 0.25) then
elseif (Part.Health.Value <= (Part:GetMass() * table.find(Module, Part.Material)) * .25) and Part.Health.Value >= 1 then
if not Part:FindFirstChildWhichIsA("Smoke") then
local Effect = Instance.new("Smoke")
Effect.Color = Color3.fromHSV(0,0,((Part.Health.Value/(Part:GetMass() * table.find(Module, Part.Material)))))
Effect.Parent = Part
Effect = nil
else
Part:FindFirstChildWhichIsA("Smoke").Color = Color3.fromHSV(0,0,((Part.Health.Value/(Part:GetMass() * table.find(Module, Part.Material))))*255)
end
if not Part:FindFirstChild("Fire1") then
local fire = game.ServerStorage.ParticleEffects.Fire1:Clone()
fire.Parent = Part
fire = nil
-- give the particle damage
end
elseif (Part.Health.Value < 1) then
wait(math.floor(Part:GetMass())*0.1)
if not Part:FindFirstChild("Explosion") then
local Explosion = Instance.new("Explosion")
Explosion.BlastRadius = Part:GetMass() * 1.7
Explosion.BlastPressure = Part:GetMass() * 360
Explosion.Parent = Part
Explosion.Position = Part.Position
wait(2)
Explosion = nil
end
end
elseif Part.Name == "Battery" then
if Part.Health.Value <= (Part:GetMass() * table.find(Module, Part.Material)) * .5 then
local Smoke = Instance.new("Smoke")
Smoke.Color = Color3.fromHSV(0,0,255*(Part.Health.Value/(Part:GetMass() * table.find(Module, Part.Material))))
Smoke.Parent = Part
Smoke = nil
end
elseif Part:IsA("VehicleSeat") then
if Part then
if Part.Health.Value <= 0 then
if Part.Occupant ~= nil then
Part.Occupant.Health = 0
end
Part.Disabled = true
end
end
if VSConnection then
VSConnection:Disconnect()
end
end
if Part.Health.Value <= 0 then
Part:BreakJoints()
if Part:FindFirstChildWhichIsA("Attachment") then
Part:FindFirstChildWhichIsA("Attachment"):Destroy()
end
Part:SetNetworkOwnershipAuto()
if Part.Name ~= "Chassis" then
Part.Parent = script.Map.Value
-- CarCheck(Car)
end
wait(3)
local light = Part:FindFirstChildWhichIsA("PointLight")
if light then
light:Destroy()
light = nil
end
local F_light = Part:FindFirstChildWhichIsA("SpotLight")
if F_light then
F_light:Destroy()
F_light = nil
end
local Fire = Part:FindFirstChild("Fire1")
if Fire then
Fire:Destroy()
Fire = nil
end
if not Car:FindFirstChildWhichIsA("VehicleSeat") then
for i,v in pairs(Car:GetDescendants()) do
if v:IsA("HingeConstraint") then
v.Enabled = false
end
if v:IsA("Sound") then
coroutine.wrap(function()
while v.PlaybackSpeed > 0.5 do
wait()
v.PlaybackSpeed = v.PlaybackSpeed - 0.05
end
while v.Volume > 0 do
wait()
v.Volume = v.Volume - 0.05
end
v:Destroy()
v = nil
end)()
end
end
end
we:Disconnect()
end
end)
end
end
end)
game.ReplicatedStorage.TriggerMod.OnServerEvent:Connect(function(Player, Toggle3, Mod)
if Toggle3 == false then
Mod:FindFirstChild("Trigger").Value = true
elseif Toggle3 == true then
Mod:FindFirstChild("Trigger").Value = false
end
print(Mod.Name.." activation has been set to "..tostring(Mod:FindFirstChild("Trigger").Value))
end)
game.ReplicatedStorage.LightEvent.OnServerEvent:Connect(function(Player, Toggle , Car)
for i,v in pairs(Car:GetDescendants()) do
if v:IsA("SpotLight") then
v.Enabled = Toggle
v.Color = v.Parent.Color
if Toggle == true then
v.Parent.Material = Enum.Material.Neon
elseif Toggle == false then
v.Parent.Material = Enum.Material.Glass
end
end
end
end)
game.ReplicatedStorage.ChangeMovement.OnServerEvent:Connect(function(Player, Toggle , Car)
for i,v in pairs(Car.VehicleSeat:GetChildren()) do
if v.Name == "Translate" or v.Name == "Deploy" then
v.Value = Toggle
print("changed movement")
end
end
end)
game.ReplicatedStorage.Brakes.OnServerEvent:Connect(function(Player, Toggle , Car)
for i,v in pairs(Car.VehicleSeat:GetChildren()) do
if v.Name == "Brakes" then
v.Value = Toggle
print("brakes are.."..Toggle)
end
end
end)