So I’am making a tower defense game where I’m currently making the placement system
So when you press a button the placeholder is supposed to appear and you can drag it around and place it down… The problem is that when you press the button it freezes your game for a solid 5 seconds and your CPU spikes before the placeholder appears.
Placeholder function:
local function createTowerPlaceholder(tower)
local towerExist = towersFolder:FindFirstChild(tower)
if not towerExist then
warn("Tower "..tower.." aint a thing")
return
end
removePlaceholder()
towerToSpawn = towerExist:Clone()
towerToSpawn.Parent = currentPlaceHolders
for i, v in ipairs(towerToSpawn:GetDescendants()) do
if v:IsA("BasePart") then
v.Transparency = 0.5
end
colorPlaceholder(BrickColor.new("Baby blue"))
end
end
Activation (the button)
testTowerButton.Activated:Connect(function()
rotation = 0
createTowerPlaceholder("Trooper")
script.select:Play()
end)
Loop for placeholder to follow mouse
runService.RenderStepped:Connect(function()
if towerToSpawn then
local result = mouseRaycast({towerToSpawn, character, currentTowers:GetChildren(), workspace.Main.ClientModels:GetChildren()})
if result and result.Instance then
if result.Instance.Parent.Name == "PlacementArea" then
canPlace = true
colorPlaceholder(BrickColor.new("Baby blue"))
else
canPlace = false
colorPlaceholder(BrickColor.new("Bright red"))
end
local x = result.Position.X
local y = result.Position.Y + 1.8 -- chnage later so its for each tower!!!
local z = result.Position.Z
local newCframe = CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation), 0)
if towerToSpawn.PrimaryPart then
towerToSpawn:SetPrimaryPartCFrame(newCframe)
else
warn("No primary part")
end
end
end
end)
why is this?