Script Timeout Issue

I can’t seem to find out how to get the script to not timeout. I’ve put wait() in numerous areas through the script, yet it still gives me the ‘Script timeout: exhausted allowed execution time’ error.

Here’s my code:

local StandArrow = script.Parent

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local itemData = require(ReplicatedStorage:WaitForChild('itemData'))
local rarityData = require(ReplicatedStorage:WaitForChild('rarityData'))

local player = game.Players.LocalPlayer

StandArrow.Parent = player.Backpack

local function StandArrowUse(player)
	print('Stand Arrow Used!')
	
	local numberSet = math.random(1, 100)
	local standAcquired = false
	local stand = nil
	
	while standAcquired == false do
		for i, v in pairs(rarityData.StandArrowRarity) do
			if math.random(1, v) == numberSet then
				standAcquired = true
				stand = i
			end
		end
	end
	
	return stand
end

StandArrow.Activated:Connect(function(player)
	if game.Players.LocalPlayer.leaderstats.Level.Value >= itemData.StandArrowUseLevel then
		StandArrow:Destroy()
		local standGot = StandArrowUse(player)
		print(standGot)
	end
end)

From what I understand, it seems to be an issue with the while loop, or so I think? But, I can’t seem to find a fix for it.

put your while loop in a coroutine so it runs asynchronously

The script timeout error occurs because your while loop is running for too long, you need a wait between each iteration for loops that you expect will run for a while, also you can wrap it in a coroutine if you dont want to yield the thread. So at the bottom of the while loop add the line:

--Add this line to the bottom of the while loop
RunService.Heartbeat:Wait()

--Add this line to where your variables are declared
local RunService = game:GetService("RunService")
1 Like

I ended up wrapping it in a coroutine and it seems to be working in terms of timing out, but now all it ever returns is nil, any idea why that may be?

1 Like

I don’t really see why your using a while loop the issue is that your for loop most likely never finds the item in the table, could you elaborate more on what your doing? Try adding a print within the if statement and see if the message gets printed. Also, based on what your code seems to be doing it should not be wrapped in a coroutine.

1 Like

Well, the idea was to have it give the player a stand, and then destroy the tool. For testing I have two stands both with a 50/50 chance, and the for loop is supposed to choose one or the other based on a number 1 - 100. I added a print in the if statement and sure enough it didn’t print. Also, I am nowhere near an advanced level scripter, so this is kind of like practice, and I have never used a coroutine in my life, and even after reading briefly over it on the Roblox Developer site I still don’t know completely how to use them or understand when they should and shouldn’t be used.

I think the issue here is that your over-complicating the probability system, since its a equal chance this will be simple to design:

local Stands = {"Example1", "Example2"}
local function StandArrowUse()
    local RandomStandIndex = math.random(1,2) --[[
        Selecting a random number between 1 and 2,
        this is equivalent to a 50% chance since you can either 
        get 1 or 2
    ]]--

   return Stands[RandomStandIndex]
end
1 Like

I have a module script which is just:

local module = {
	
	['StandArrowRarity'] = {

		['TheWorld'] = 50;
		['StarPlatinum'] = 50;

	}
}

return module

How would I be able to integrate this into an easy-to-set-up 1 - 100 kind of fashion? As you can see I just put it 50% chance for both of them, so is there any way I could do it the way I am or would I have to do the 1, 2 kind of way?

The 50/50 chance to get the two stands was just a placeholder to see if I could even script this whole thing, so later on once I get past this the idea would be to change the chances and such, in a simple and organized way.

that is not necessary, but you would need to sum of table and have defined ranges for each item, for example between 1-50 is for “The World” and 51-100 is for “StarPlatinum”, again this will not produce different results but just give yourself more work to do, since it s a 50 to 50 chance, weights are generally use for more complex probability systems.

1 Like

Alright, that makes sense, sorry for dragging this on for so long, and thank you for the help!

1 Like