You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
i would like the code to run without crashing -
What is the issue? Include screenshots / videos if possible!
it crashes -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i have tried replacing task.wait with wait
for some odd reason it only occurs with one my my towers in my tower defense game
here is my code:
local Tower = {}
local RunService = game:GetService("RunService")
local TowerModule = require(game.ReplicatedStorage:WaitForChild("Tower Module"))
local EnemyPlacementModule = require(game.ServerScriptService:WaitForChild("Enemy Placement Module"))
function Tower.Run(Tower)
local UpgradedEvent = Tower:WaitForChild("Upgraded")
local Targeting = false
local Target = nil
local Connection
local ProjectileOrigins = {}
local Data = TowerModule.GetData(Tower)
for i, instance in ipairs(Tower:GetChildren()) do
if instance.name == "Fire Part" then
table.insert(ProjectileOrigins, {instance, Tower.Humanoid:LoadAnimation(instance.Fire)})
end
end
local NextOrigin = ProjectileOrigins[1]
local function FindTarget()
local Farthest = {}
local First = true
for Enemy, Time in pairs(EnemyPlacementModule) do
if Enemy.Parent and (Tower.PrimaryPart.Position - Enemy.PrimaryPart.Position).Magnitude <= Data.Range then
if First then
First = false
Farthest = {Enemy, Time}
elseif Time > Farthest[2] then
Farthest = {Enemy, Time}
end
end
end
Target = Farthest[1]
end
local function Attack()
local Index = 0
while Target do
Index += 1
print("Loop", Index)
Targeting = true
local BasePosition = Target.PrimaryPart.Position
Tower:PivotTo(CFrame.lookAt(Tower.PrimaryPart.Position, Vector3.new(BasePosition.X, Tower.PrimaryPart.Position.Y, BasePosition.Z)))
local Bullet = Instance.new("Part")
Bullet.Name = "Bullet"
Bullet.Size = Vector3.new(0.1, 0.1, (NextOrigin[1].Position - Target.PrimaryPart.Position).Magnitude)
Bullet.Anchored = true
Bullet.CanCollide = false
Bullet.CanTouch = false
Bullet.CanQuery = false
Bullet.Color = Color3.new(1, 1, 0)
Bullet.CFrame = CFrame.lookAt(NextOrigin[1].Position, Target.PrimaryPart.Position)
Bullet.CFrame = Bullet.CFrame:ToWorldSpace(CFrame.new(Vector3.new(0, 0, -Bullet.Size.Z / 2)))
Bullet.Parent = workspace
game.Debris:AddItem(Bullet, 0.05)
NextOrigin[2]:Play()
if #ProjectileOrigins > 1 then
NextOrigin = ProjectileOrigins[table.find(ProjectileOrigins, NextOrigin) + 1] or ProjectileOrigins[1]
end
Target.Humanoid:TakeDamage(Data.Damage)
print("After humanoid Damage") -- Breakpoint here
task.wait(1) -- Crashes here?
print("Pre function call") -- Breakpoint here (never happens because of crash)
FindTarget()
print("Atfer function call")
end
Targeting = false
Connection = RunService.Heartbeat:Connect(function()
FindTarget()
if Target then
Connection:Disconnect()
Attack()
end
end)
end
UpgradedEvent.Event:Connect(function()
Data = TowerModule.GetData(Tower)
end)
Connection = RunService.Heartbeat:Connect(function()
FindTarget()
if Target then
Connection:Disconnect()
Attack()
end
end)
Tower.ClickDetector.MouseClick:Connect(function(Player)
game.ReplicatedStorage["Activate GUI"]:FireClient(Player, Tower)
end)
end
return Tower