So in my tower defense game i have a tower that summons a unit in a specified position, and it works well most of time, BUT NOT THE WHOLE TIME. As i was playing, i click the towers ability to summon and it does summon, but the when it summons, the script sometimes stop without even a error, i tried the :WaitForChild() Methods and they make the tower work but they do not assist the help of it, how do i fix so they don’t stop when spawning a unit?
local char = script.Parent.Parent.TowerValue.Value
local creator_player = char.creator
local Players = game.Players
local creator = Players:FindFirstChild(creator_player.Value)
local humanoid = char.Humanoid
local w = humanoid:LoadAnimation(char.build)
script.Parent.MouseButton1Click:Connect(function()
if creator.leaderstats.Coins.Value >= 0 and char.SentriesAdded.Value < 1 and creator ~= nil then
wait()
local sentry = game.ReplicatedStorage.Towers.Sentry:Clone()
sentry.Parent = workspace.Towers
sentry:WaitForChild("creator").Value = char.creator.Value
char:WaitForChild('SentriesAdded').Value = char:WaitForChild('SentriesAdded').Value + 1
creator:WaitForChild('leaderstats').Coins.Value -= 0
sentry:WaitForChild("Placed").Value = true
sentry:WaitForChild("PartialCreator").Value = char
sentry:MoveTo(char:WaitForChild('SentryPosition').Position)
char:WaitForChild('Torso').CFrame = CFrame.new(char:WaitForChild('Torso').Position,sentry:WaitForChild('turretBase').Position)
script.Parent.Parent:WaitForChild('CurrentSentry').Value = sentry
char:WaitForChild("IsBuilding").Value = true
sentry:WaitForChild('Part2'):WaitForChild("BuildEffect").Enabled = true
sentry:WaitForChild('Part1').Transparency = 0.55
sentry:WaitForChild('Part2').Transparency = 0.55
sentry:WaitForChild('Part3').Transparency = 0.55
char:WaitForChild('Torso').deploy:Play()
w:Play()
char:WaitForChild('Items')["Auto Shotgun"].Transparency = 1
w.Stopped:Wait()
sentry:WaitForChild('Part1').Transparency = 0
sentry:WaitForChild('Part2').Transparency = 0
sentry:WaitForChild('Part3').Transparency = 0
print(w.Length)
sentry.Part2:WaitForChild("BuildEffect").Enabled = false
sentry:WaitForChild("IsBuilding").Value = false
char:WaitForChild('Torso').CFrame = CFrame.new(char:WaitForChild('Torso').Position,char:WaitForChild('NormalPosition').Position)
char:WaitForChild('Items')["Auto Shotgun"].Transparency = 0
w:Stop()
wait()
else
print('Failed to add sentry.')
end
end)