Looping Crashing the Game

Someone Knows why this script is crashing the game?

function Droppers:Init(tycoon)
	coroutine.wrap(function()
		while true do
			task.wait(Rate)
			Droppers:Drop()
		end
	end)()
end	
	
function Droppers:Drop(tycoon)
	
	local CollectionService = game:GetService("CollectionService")
	for i, Dropper in pairs(CollectionService:GetTagged("Dropper")) do
	    Rate = Dropper:GetAttribute("Rate")
		local Type = Dropper:GetAttribute("Type")
		
		for _, Drop in pairs(CollectionService:GetTagged("Drop")) do
			
		local Type2 = Drop:GetAttribute("Type")	
			
			if Type == Type2 then
				
				local Cdrop = Drop:Clone()
				Cdrop.Parent = Dropper
				Cdrop.Position = Dropper.Dropper.Pos.Position
			end
		end
		
	end
	
	
	
	
	
	
	return Rate
end

When “while true do” loops don’t have a wait ,the game crashes because it’s trying to do something an infinite amount of times in an Instance.

There doesn’t seem to be a reference or variable to the ‘Rate’ in the first function so it probably didn’t count it as a wait , have you tried replacing it with a number and see what happens?

3 Likes

I did a return Rate on the end of the drop function, but i Will try this.

Yea if Rate don’t have a pre-declared value before you run Droppers:Drop() it will crash due to it running an infinite amount of time. Plus Droppers:Drop doesn’t even changes Rate since there are no variable for the returning parameter.

Do this:

function Droppers:Init(tycoon)
	coroutine.wrap(function()
		while true do			
			DropperRate = Droppers:Drop() --since Rate is already a pre-existing variable.
                        task.wait(DropperRate)
		end
	end)()
end	
2 Likes

I changed Rate for a number and this didnt work

Like @boterflic5 mentioned, you need to add a wait() procedure to your coroutine.

Try something like this:

function Droppers:Init(tycoon)
    coroutine.wrap(function()
        while true do
            task.wait(Rate)
            Droppers:Drop()
            wait()
         end
    end)()
end
2 Likes

this also didn’t work, for any reason the game crashed earlier

Hm, that’s interesting. Could you try a workaround by yielding and resuming the coroutine?

local task = coroutine.create(function()
    Droppers:Drop()
end

while true do
    coroutine.resume(task)
    coroutine.yield(task)
    wait(Rate)
end
1 Like

I didn’t understood very well this script so I just copy and pasted and also didn’t work

Also didn’t work, but the game crashed earlier

Hm. The only other issue I can think of is that your program hangs on the second function.

Perhaps try an equality check when iterating through instances to check whether Drop is of a certain class, using IsA("ClassName").

1 Like

Maybe it’s a problem with the script who calls the module?

local Tycoon = require(game:GetService("ServerScriptService").General)
local doro =  script.Parent.Doro
local text = script.Parent.Parent.BillboardGui.TextLabel
local EFteam = game:GetService("Teams")["Explosive Flash"]
local Buttons = require(game:GetService("ServerScriptService").Components.Buttons)
local EFtycoon = game:GetService("ServerStorage").Tycoons["Explosive Flash"]
local db = false
local EFwtyccon = script.Parent.Parent
local Owner = EFwtyccon:GetAttribute("Owner")
local Droppers = require(game:GetService("ServerScriptService").Components.Droppers)


Tycoon.new(EFwtyccon)

doro.CanCollide = false

	
 doro.Touched:Connect(function(player)
        Tycoon:Init(player,doro, text,EFteam,EFwtyccon)
	    Buttons:New(EFwtyccon)   
	if doro.Parent.Parent:GetAttribute("Owner")	~= nil
	then
		EFwtyccon:SetAttribute("Owner",true)
		Buttons:Init("1buttonEF", EFwtyccon.Purchables)
		Droppers:new(EFwtyccon)
		Droppers:Init(EFwtyccon)
		Buttons:Buy1(EFwtyccon)
		Buttons:Buy2(EFwtyccon)
		doro.CanTouch = false
		
	end	
 end)

ok, I will try verify if the drop is a part

I’m not seeing any glaring mistakes there with your function calls. Again, you could try verifying their type using a typechecker?

Similar to:

--!strict

local Tycoon : Model = ...
local doro : BasePart = ...
local text : TextLabel = ..
-- etc.

See what you end up with!


Should add that a typechecker in LuaU will accomplish something similar to assert(), where it will throw an error (if in !strict mode if a variable isn’t of a certain class.

1 Like

On the module script I changed one line to

if Type == Type2 and Drop:IsA("Part")

but the game still crashes and return an error “attempt to call a thread”

sorry, i didn’t understood, how do I use this?

To answer both of your posts:

1:

So it’ll be an issue with your corotuine then. A thread essentially is a line of execution, coroutines are temporarily second threads that return to the main program.

For some reason, your script believes that you’re trying to call a coroutine instead of a function.


2:

Type-checking allows you to track variables by class when assigning and passing to functions, like so:

local myVar : Class = ...

-- A script would be

local myScript : Script = script

-- If the script was a child of a part

local myPart : BasePart = script.Parent

-- For functions

local function(arg1 : BasePart, arg2 : Model)
    print(type(arg1), type(arg2))
end
1 Like

That script wasn’t working so I gave up and make another but now it doesn’t doesn’t crash immediately but create many droops ignoring the wait (I put a wait on the while script) and without being at the position I want, you know howcan I fix this?

function Droppers:Drop(tycoon)
	
	local CollectionService = game:GetService("CollectionService")
	for _, Dropper in pairs(CollectionService:GetTagged("Dropper")) do
	for _, Drop in pairs(CollectionService:GetTagged("Drop")) do
	
	local Type = Dropper:GetAttribute("Type")
	local Type2 = Drop:GetAttribute("Type")
	local Rate = Dropper:GetAttribute("Rate")
	
	
	if Type == Type2 and Dropper:IsDescendantOf(workspace) then
				
		wait(Rate)		
		local Cdrop = Drop:Clone()
		Cdrop.Position = Dropper.Dropper.Pos.Position
		Cdrop.Parent = Dropper.Parent
		
			end
		end
		
	end
	end

Are you sure you’ve used SetAttribute() to change the value of Rate?

Try printing it and seeing what it returns.

1 Like

It’s print 2 and nil, and the drops were not created cotinuosly, they few very quick, stop and continue spawninh.

Maybe the best way to do that is putting script on every dropper instead of using tags?