Hello everyone! So i’ve been trying to make turret with module script, and it should work fine, but for some reason when i try using wait or task wait the loop just ends inside task.spawn.
Here’s the script fragment:
PLEASE READ ENTIRE POST WITH NOTES BELOW THE CODE!!!
turret_module.Idle = function(turret_model)
print("runned")
turret_module.cancel_task()
wait(0.5)
current_task = task.spawn(function()
print("Started task")
task.spawn(function()
print("Starting loop task")
while true do
print("loop")
wait(0.1)
print("loop2")
local targets = turret_module.get_avaiable_targets(turret_model)
print(targets)
if #targets > 0 then
turret_module.Spotted(turret_model)
end
end
end)
print("Starting idle animation")
while true do
print("anim")
wait()
print("anim2")
local tween1 = TweenService:Create(turret_model.Main, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Orientation = turret_model:GetAttribute("main_rot") + Vector3.new(0, -20, 0)})
tween1:Play()
wait(8)
local tween2 = TweenService:Create(turret_model.Main, TweenInfo.new(5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Orientation = turret_model:GetAttribute("main_rot") + Vector3.new(0, 20, 0)})
tween1:Play()
wait(8)
end
end)
end
so when the function rans it just prints out:
- started task
- starting loop task
- loop
- starting idle animation
- anim
as you can see it doesnt type loop2 or anim2 which are AFTER the wait, so it means that code doesn’t run for some reason. When i were debugging i added wait(0.5) before the task spawn and what i got is that the code were running like couple times before the 0.5 wait passed even tho there is no way it could repeat. But anyway, i were thinking "oh then cuz of the repeative thingy the task cancels or something so it rans and instantly cancels, so i deleted the cancel_task line of code, but it still happened! I don’t see anything canceling the task so it shouldnt do so. Here is the code running that function:
turret_module.Create = function(pos: Vector3, rotation: Vector3, fov: number, distance: number)
print("runned create function")
if not fov then fov = 60 end
if not distance then distance = 100 end
if not rotation then rotation = Vector3.new(0,0,0) end
local turret_model = ServerStorage.Turret:Clone()
turret_model:SetAttribute("FOV", fov)
turret_model:SetAttribute("distance", distance)
turret_model:SetAttribute("main_rot", rotation)
turret_model:PivotTo(CFrame.new(pos) * CFrame.Angles(rotation.X, rotation.Y, rotation.Z))
turret_model.Parent = workspace.Turrets
turret_module.Idle(turret_model)
end
this function shouldnt run few times too, only once, but the print happens couple times in same interval before the 0.5 wait time passes in idle function. Im confused whats going on, i even can give you the code running that creatre function.
script.Parent.Touched:Connect(function(hit)
local found = false
if hit.Parent:FindFirstChild("Humanoid") and found == false then
found = true
require(game.ServerScriptService.Modules.Turret).Create(Vector3.new(0,3,0), Vector3.new(0,0,0), 60, 100)
script:Destroy()
end
end)
as you can see i tried doing everything so it doesnt run a couple times. please help, im very confused!