I’ve been working on a TD game, but testers has been reporting of numerous FPS drops when placing too many towers. This script, handling the tower placement, was the cause of the FPS drops, taking up 1% to 66% of the script performance as more towers are being placed by that player. How can I optimize this? Do I need to change it entirely?
local UIS = game:GetService("UserInputService")
local Unit = nil
local player = game.Players.LocalPlayer
local _Config = script.Parent.Parent.Parent.Configuration
local Remote = game.ReplicatedStorage.ReplicatedStorage_CLOUD.UnitControls.PlaceUnit
local Mouse = player:GetMouse()
for i, v in pairs(script.Parent:GetChildren())do
if v:IsA("Frame") and v:FindFirstChild("Place") then
v:FindFirstChild("Place").MouseButton1Up:Connect(function()
_Config.UnitChoosed.Value = v:WaitForChild("Place"):WaitForChild("Tower").Value
_Config.UnitEditor.Value = true
Unit = _Config.UnitChoosed.Value:Clone()
Unit.Parent = game.Workspace
Mouse.TargetFilter = Unit
local AnimationHandler = Unit:WaitForChild("Visual"):WaitForChild("AnimationController"):LoadAnimation(Unit:WaitForChild("Visual"):WaitForChild("Animations"):WaitForChild("Hold"))
AnimationHandler:Play()
game:GetService("RunService").RenderStepped:Connect(function()
if Mouse.Target ~= nil then
Unit.PrimaryPart.CFrame = CFrame.new(Mouse.Hit.Position) * CFrame.new(0, Unit.Hitbox.Size.Y/2, 0) -- Offset
end
end)
Mouse.Button1Down:Connect(function()
Remote:FireServer(_Config.UnitChoosed.Value, Mouse.hit, _Config.UnitOrientation.Value, _Config.UnitPlaceable.Value)
Unit:Destroy()
end)
end)
end
end