I have took the challenge of making a basic tycoon and I was doing well until I figured I couldn’t set my AutoDrop to false or true.
Heres the code used in the main module for testing:
function Dropper:AutoSpwanParts(CoolDownTime : number, CanAuto : boolean)
print("Cool down time: " .. tostring(CoolDownTime) .. ", Can auto: " .. tostring(CanAuto))
if typeof(CoolDownTime) ~= "number" then warn("CoolDownTime not a number! Try again.") return end
if CanAuto == nil then CanAuto = true end
repeat
local Part = self.StartPos:Clone()
local Folder = self.ClonedPartFolder
Part.CFrame = self.StartPos.CFrame
Part.Anchored = false
Part.CanCollide = true
Part.CanTouch = true
Part.Name = "Part " .. tostring(self.ClonedPartsNum)
Part:SetAttribute("BaseNumber", self.BaseNumber)
Part.Parent = Folder
self.ClonedPartsNum += 1
wait(CoolDownTime)
until not CanAuto
end
When setting the CanAuto it will print the message. I tried this and it only prints the first thing it was set to (1 and true) then the code below that won’t run (Including print statements). This means setting the AutoDrop makes the code below it not run but why?
alright then i understand what do you want to get as results now
--dropper script
function Dropper:AutoSpwanParts(CoolDownTime : number)
print("Cool down time: " .. tostring(CoolDownTime) .. ", Can auto: ")
if typeof(CoolDownTime) ~= "number" then warn("CoolDownTime not a number! Try again.") return end
repeat
local Part = self.StartPos:Clone()
local Folder = self.ClonedPartFolder
Part.CFrame = self.StartPos.CFrame
Part.Anchored = false
Part.CanCollide = true
Part.CanTouch = true
Part.Name = "Part " .. tostring(self.ClonedPartsNum)
Part:SetAttribute("BaseNumber", self.BaseNumber)
Part.Parent = Folder
self.ClonedPartsNum += 1
task.wait(CoolDownTime)
until nil
end
--other script
local SpawnLoop=task.spawn(function()
self.DropperClass:AutoSpwanParts(10)
end)
task.wait(1)
coroutine.close(SpawnLoop) --stop the loop from dropping parts after 1 second
did you want something like this? of course you can customize this and stop the loop whenever you want to
Removing the CanAuto param makes the function kind of useless (Sorry for language) to me because the CanAuto is supposed to control if parts can repeatedly spawn or not. For example, if a player leaves the server or the tycoon isn’t claimed then you wouldn’t want parts dropping all the time huh.
however if you prefer less nerdy way of stopping your loop here another version of your thing (this one is player related)
Dropper={}
Dropper.PlayerData={}
function Dropper:AutoSpwanParts(CoolDownTime : number,plr)
print("Cool down time: " .. tostring(CoolDownTime) .. ", Can auto: ")
Dropper.PlayerData[plr.UserId]={IsSpawning=true}
if typeof(CoolDownTime) ~= "number" then warn("CoolDownTime not a number! Try again.") return end
task.spawn(function()
repeat
local Part = self.StartPos:Clone()
local Folder = self.ClonedPartFolder
Part.CFrame = self.StartPos.CFrame
Part.Anchored = false
Part.CanCollide = true
Part.CanTouch = true
Part.Name = "Part " .. tostring(self.ClonedPartsNum)
Part:SetAttribute("BaseNumber", self.BaseNumber)
Part.Parent = Folder
self.ClonedPartsNum += 1
task.wait(CoolDownTime)
until Dropper.PlayerData[plr.UserId].IsSpawning==false
end)
end
Dropper:AutoSpwanParts(10,player)
task.wait(1)
Dropper.PlayerData[player.UserId].IsSpawning=false --kill the loop from spawning parts
I even made third version which is even alot easier than the last one (if you didnt understand last one)
function Dropper:AutoSpwanParts(CoolDownTime : number)
print("Cool down time: " .. tostring(CoolDownTime) .. ", Can auto: ")
if typeof(CoolDownTime) ~= "number" then warn("CoolDownTime not a number! Try again.") return end
return task.spawn(function()
repeat
local Part = self.StartPos:Clone()
local Folder = self.ClonedPartFolder
Part.CFrame = self.StartPos.CFrame
Part.Anchored = false
Part.CanCollide = true
Part.CanTouch = true
Part.Name = "Part " .. tostring(self.ClonedPartsNum)
Part:SetAttribute("BaseNumber", self.BaseNumber)
Part.Parent = Folder
self.ClonedPartsNum += 1
task.wait(CoolDownTime)
until nil
end)
end
local AutoDropping=Dropper:AutoSpwanParts(10)
if coroutine.status(AutoDropping)=="suspended" then --if the dropper is dropping
coroutine.close(AutoDropping)--stop it from auto dropping
end
First of all, I do understand both of your code blocks. Secondly, your first code block wouldn’t work because every time you call the method it would create a new thread every time it’s called. The second one might work so amma try it and then inform you if it doesn’t work.
if self.DropTask and not CanAuto then
task.cancel(self.DropTask)
elseif not self.DropTask and CanAuto then
print("Task started!")
self.DropTask = task.spawn(function()
repeat
local Part = self.StartPos:Clone()
local Folder = self.ClonedPartFolder
Part.CFrame = self.StartPos.CFrame
Part.Anchored = false
Part.CanCollide = true
Part.CanTouch = true
Part.Name = "Part " .. tostring(self.ClonedPartsNum)
Part:SetAttribute("BaseNumber", self.BaseNumber)
Part.Parent = Folder
self.ClonedPartsNum += 1
task.wait(CoolDownTime)
until false
end)
end