-
What do you want to achieve? An optimized vehicle damage system
-
What is the issue? I need to find alternatives that would be better than touched events
-
What solutions have you tried so far? Tried optimizing, but I need ideas for what I could use instead of touched events since I know those could be laggy
local Settings = require(game.ServerStorage:WaitForChild("VehicleDamageSettings"))
if script.Parent.MaxVehicleHealth.Value==0 then
script.Parent.MaxVehicleHealth.Value=(Settings.DefaultVehicleHealth)*(script.Parent.HealthMultiplier.Value)
end
script.Parent.VehicleHealth.Value=script.Parent.MaxVehicleHealth.Value
local function DisableVehicle()
script.Parent["Wheel Lock"].CanCollide=true
while script.Parent.Damage.Velocity.Magnitude>5 do
wait(1)
end
script.Parent.Parent.BumperPart.Anchored=true
if script.Parent.Parent.Parent.DriveSeat.Occupant then
script.Parent.Parent.Parent.DriveSeat.Occupant.Sit=false
end
for v, wheels in pairs(script.Parent.Parent.Parent.Wheels:GetDescendants()) do
if wheels:IsA("BasePart") then
wheels.Anchored=true
end
end
end
local deb = false
for _, child in pairs(script.Parent:GetChildren()) do
if child:IsA("BasePart") and Settings.DamageSystemStatus==true and child.Name=="PitPartL" or child.Name=="PitPartR" or child.Name=="PitBar" or child.Name=="Damage" then
child.Touched:Connect(function(touchedpart)
if deb==false and touchedpart.CanCollide==true then
deb = true
local chspeed = (child.Velocity.Magnitude)*0.9144
local tgspeed = (touchedpart.Velocity.Magnitude)*0.9144
if tgspeed > 40 then
if not (touchedpart:FindFirstAncestor("Body")) then
print(touchedpart)
if not touchedpart:FindFirstAncestor("Wheels") then
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(tgspeed/4)
else local tpbody = (touchedpart:FindFirstAncestor("Body"))
if tpbody ~= script.Parent:FindFirstChild("Body") then
local tpmult = tpbody:FindFirstChild("MaxVehicleHealth", true)
local multiplier = (tpmult.Value)/(script.Parent.MaxVehicleHealth.Value)
--print(tpbody.Name)
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(tgspeed * multiplier)
end
end
end
elseif chspeed > 40 and (touchedpart.Parent:FindFirstChild("Humanoid")) and game.ServerStorage:WaitForChild("VehicleDamageSettings").HitAndRun.Value==true then
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(chspeed/5)
touchedpart.Parent.Humanoid:TakeDamage(chspeed * (math.random(0.75,1.5)))
touchedpart.Parent.Humanouid.Sit=true
print(touchedpart)
if touchedpart:IsA("Part") and not touchedpart:FindFirstAncestor("Body") then
local tpmult = 800
local multiplier = (tpmult.Value)/(script.Parent.MaxVehicleHealth.Value)
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(chspeed * multiplier)
end
elseif chspeed > 40 then
if not (touchedpart:FindFirstAncestor("Body")) then
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(chspeed)
else local tpbody = (touchedpart:FindFirstAncestor("Body"))
if touchedpart:IsDescendantOf(script.Parent.Parent) then return end
print(touchedpart)
if tpbody ~= script.Parent:FindFirstAncestor("Body") then
local tpmult = tpbody:FindFirstChild("MaxVehicleHealth", true)
local multiplier = (tpmult.Value)/(script.Parent.MaxVehicleHealth.Value)
script.Parent.VehicleHealth.Value=script.Parent.VehicleHealth.Value-(chspeed * multiplier)
end
end
end
wait(5)
deb = false
end
end)
end
end
local deb2=false
script.Parent.VehicleHealth.Changed:connect(function()
local soundnum = math.random(1,2)
local soundname = "Crash"..soundnum
local sound = (script.Parent.Effect1:FindFirstChild(soundname))
sound:Play()
if script.Parent.VehicleHealth.Value <= 0 and script.Parent.Effect1.FireA.Enabled==false and deb2==false then script.Parent.VehicleHealth:Destroy()
deb2=true
wait(5)
if script.Parent.OnFire.Value==true then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") or effect:IsA("Fire") then
if effect.Name=="Smoke1" or effect.Name=="FireA" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
script.Parent.Effect1.EngineExplosion:Play()
script.Parent.Parent.Parent.DriveSeat:Destroy()
DisableVehicle()
wait(math.random(Settings.MinResponseTime, Settings.MaxResponseTime))
local firesize = script.Parent.Effect1.FireA.Size
if firesize>2 then
local willitexplode = math.random(1, Settings.ExplosionChance)
if willitexplode == 1 then
local exp = Instance.new("Explosion")
exp.BlastPressure=0
exp.BlastRadius=40
exp.ExplosionType=Enum.ExplosionType.NoCraters
exp.DestroyJointRadiusPercent=1
exp.Position=script.Parent.Damage.Position
exp.Parent=script.Parent.Effect1
script.Parent.Effect1.Explosion:Play()
exp.Hit:Connect(function(hit, distance)
if hit.Name == "HumanoidRootPart" and hit.Parent:FindFirstChild("Humanoid") then
if distance < 20 then
hit.Parent.Humanoid:TakeDamage(110)
else local playerdamage = (1/(distance))*1000
hit.Parent.Humanoid:TakeDamage(playerdamage)
end
end
end)
for v, carpart2 in pairs(script.Parent.Parent.Parent:GetDescendants()) do
if carpart2:IsA("BasePart") then
carpart2.Material=Enum.Material.CorrodedMetal
carpart2.BrickColor=BrickColor.new("Black")
elseif carpart2:IsA("Decal") then
carpart2:Destroy()
end
end
end
wait(30)
if script.Parent.Effect1.FireA.Size>0 then
script.Parent.Effect1.FireA.Enabled=false
script.Parent.Effect2.FireA.Enabled=false
script.Parent.Effect3.FireA.Enabled=false
script.Parent.Effect1.Smoke1.Enabled=false
script.Parent.Effect2.Smoke1.Enabled=false
script.Parent.Effect3.Smoke1.Enabled=false
end
script.Parent.Effect1.Smoke3.Enabled=true
for i = 1, 10 do
wait(10)
script.Parent.Effect1.Smoke3.Size = script.Parent.Effect1.Smoke3.Size-0.1
end
end
script.Parent:Destroy()
elseif script.Parent.OnFire.Value==false then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") then
if effect.Name=="Smoke1" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
script.Parent.Parent.Parent.DriveSeat:Destroy()
DisableVehicle()
wait(180)
script.Parent.Effect1.Smoke1.Enabled=false
script.Parent.Effect2.Smoke1.Enabled=false
script.Parent.Effect3.Smoke1.Enabled=false
script.Parent.Effect1.Smoke3.Enabled=true
for i = 1, 10 do
wait(10)
script.Parent.Effect1.Smoke3.Size = script.Parent.Effect1.Smoke3.Size-0.1
end
script.Parent:Destroy()
end
elseif script.Parent.VehicleHealth.Value < 100 then
local firechance = (math.random(1, Settings.FireChance))
for v, effect in pairs(script.Parent:GetDescendants()) do
if firechance==1 then
if effect:IsA("Smoke") or effect:IsA("Fire") then
script.Parent.OnFire.Value=true
if effect.Name=="Smoke2" or effect.Name=="FireB" then
effect.Enabled=true
else effect.Enabled=false
end
end
elseif firechance~=1 then
if effect:IsA("Smoke") or effect:IsA("Fire") then
if effect.Name=="Smoke2" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
end
elseif script.Parent.VehicleHealth.Value < 225 then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") or effect:IsA("Fire") then
if effect.Name=="Smoke3" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
elseif script.Parent.VehicleHealth.Value < 400 then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") or effect:IsA("Fire") then
if effect.Name=="Smoke4" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
elseif script.Parent.VehicleHealth.Value < 450 then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") or effect:IsA("Fire") then
if effect.Name=="Smoke5" then
effect.Enabled=true
else effect.Enabled=false
end
end
end
elseif script.Parent.VehicleHealth.Value >= 700 then
for v, effect in pairs(script.Parent:GetDescendants()) do
if effect:IsA("Smoke") or effect:IsA("Fire") then
effect.Enabled=false
end
end
end
end)
``’