OOP Item spwan issue

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:

self.DropperClass:AutoSpwanParts(1, true)
wait(1)
self.DropperClass:AutoSpwanParts(1, false)

Heres the Dropper module method:

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?

This means setting the AutoDrop makes the code below it not run but why?

until not CanAuto

this means if you call it like this

Dropper:AutoSpwanParts(1,true)

it wont spawn anything

i kinda dont understand what do you mean but uh if you want to make it so it spawns when set to true change it to this:

until CanAuto
1 Like

If I do that and set the AutoSpwan to “true” it won’t work because the until is true (When the until is true, the code block won’t run)

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

this will stop spawning items after 1 second

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.

well yes but if you do this

coroutine.close(SpawnLoop)

it will stop the parts from spawning immediately

and my code is better than the one with CanAuto because it doesnt block the code execution and continues even after executing the loop

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.

1 Like

Solution found:

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.