#Error in For loop

77 messages · Page 1 of 1 (latest)

upbeat sequoia
#

I get the error invalid 'for' limit error, dunno what it is, dunno how to fix it lol

#

I tried adding a or 0 but then the script just doesnt run at all

spare grotto
#

just do in the loops

upbeat sequoia
#

Error in For loop

spare grotto
#

for i, _ ...

#

try that

#

for i, _ in slimeList do

#

the another loop too

upbeat sequoia
spare grotto
#

yes but in ALL for loops

#

i think the for loops needs in LUAU always an index

#

so for i, _

spare grotto
upbeat sequoia
upbeat sequoia
#

well for loops dont require indexes

#

cause line 87 is the last line to run and its the only giving an error

spare grotto
#

is counterdraw a table?

spare grotto
upbeat sequoia
#

no its a number

#

a counter

#

that goes up and down

spare grotto
#

you cant iterate over a number so

#

and there is saying

#

attempt to iterate over a nil value

#

the value is nil

#

for only works on table tho

#

and doing like

upbeat sequoia
#

well slimeList is a table

spare grotto
#

for i = 1, 10 ...

thigns like that

spare grotto
upbeat sequoia
#

slimeList[counterDraw] is meant to get the value from slimelist at the counterDraw position

spare grotto
#

yes but is counterdraw goin to be a table?

upbeat sequoia
#

no

#

why would it?

spare grotto
#

then u cant iterate over it

#

because you are doing

#

for i, _ in counterdraw

upbeat sequoia
#

no its for i,_ in slimeList[counterDraw]

spare grotto
#

yeah you're accessing to the counterDraw in the slimelist table

#

but counterDraw AINT a table

upbeat sequoia
#

so then how would you change it

#

so that it repeats slimeList[counterDraw] times

spare grotto
#

prolly i'll change the for loop because i dont even know what you're trying to do

#

well just do

#

for i = 1, slimeList[counterDraw] do

upbeat sequoia
spare grotto
#

counterDraw is NIL

upbeat sequoia
#

yeah its 0

spare grotto
#

no, its NIL

#

if it is 0 it doesnt run

upbeat sequoia
spare grotto
#

i see many problems here of logic

#

You're accessing to that table with counterDraw, that is 0, and theres NO thing that is 0

upbeat sequoia
#

All I need is for it to run for however many times the value of the slimelist is, if its 0 it skips it

spare grotto
#

just do for i = 1, counterDraw do ...

upbeat sequoia
#

of counterDraw

#

slimeList[counterDraw}
if counterDraw was 1
and slime1= 3 the loop would need to run 3 times

spare grotto
#

yeah then u may re write some things

#

just rest a bit and think about it when u want i cant help without knowing anything more

upbeat sequoia
upbeat sequoia
spare grotto
#

i cant do shit without understanding what you're trying to do

#

u cant stay here sitted waiting for someone

upbeat sequoia
#

I gave you the entire code

spare grotto
#

go on and research

spare grotto
#

if i was you just re write the entire thing in a chill moment and understanding what im doing

upbeat sequoia
spare grotto
#

you was trying for example accessing to the table with a value that doesnt exist in the table, for loops confusing idk

#

is just to make a look

upbeat sequoia
#
-- Sorry if this script isnt the most optimized its my first time doing a system like this lol

local model  = script.Parent
local addSlime = model:WaitForChild("AddSlime")
local mergeSlimes = model:WaitForChild("MergeSlimes")
local Slimes =  model:WaitForChild("Slimes")
local cubby =  model:WaitForChild("Cubby")
local slimeStorage = model:WaitForChild("SlimeStorage")
local slimeCounter = 0
local distanceBetweenSlimes = 2 --Amount of studs between each slime horizontaly
local touching = false

local money = 1000 -- change to wherever you have the money system at
local slimeCost = 1 -- initial slime cost
local slimeCostModifier = 1.15 -- What to  multiply the price by each time
local mergeAmount = 3 --change to however many are needed to merge
local slimePerLayer = 5-- self explanetory

local slimeList = {-- amount of these dont matter just put all the slimes you have in the Slimes folder
    ["slime1"] = 1,
    ["slime2"] = 0, -- Just add a new variable like this for each new slime along with each slime model in the Slimes folder not slimestorage
    ["slime3"] = 0,
    ["slime4"] = 0,
    ["slime5"] = 0,
    ["slime6"] = 0,
    ["slime7"] = 0,
    ["slime8"] = 0,
    ["slime9"] = 0,
    ["slime10"] = 0,
    ["slime11"] = 0,
}

addSlime.Touched:Connect(function()
    if touching == false then
        touching = true
        if money >= slimeCost then
            money -= slimeCost
            slimeList.slime1 += 1
            slimeCost = slimeCost * slimeCostModifier 
            draw()
        end
    end
end)
addSlime.TouchEnded:Connect(function()
    touching = false
end)

mergeSlimes.Touched:Connect(function()
    local counterMerge = 0
    for _ in slimeList do
        counterMerge += 1
        if slimeList[counterMerge] >= mergeAmount then
            slimeList[counterMerge + 1] += math.floor(slimeList[counterMerge]/mergeAmount)
        end
    end
    draw()
end)

function draw()
    print("Drawing")
    local counterDraw = 0
    local layerCounter = 0
    local X = Slimes.StartPos.Position.Z-- this depends on the orientation you can change the z to a x if you orient it differently I think
    local Y = Slimes.StartPos.Position.Y
    for _ in pairs(Slimes:GetDescendants()) do
        counterDraw += 1
        print("Counter Draw adding")
    end
    for _ in slimeList do
        if layerCounter == slimePerLayer then
            layerCounter -= slimePerLayer
            local cubbyClone = cubby:Clone()
            cubbyClone.Parent = cubby.Parent
            cubbyClone.Position =cubby.Position + Vector3.new(0,cubby.Size.Y,0)
            Y += cubby.Size.Y
            X = Slimes.StartPos.Position.Z -- same as last comment on orientation
        end
        if slimeList[counterDraw] ~= nil then
            for i=1,slimeList[counterDraw] do
                print("Cloning")
                layerCounter += 1
                local Clone  = Slimes:WaitForChild("Slime " .. counterDraw):Clone()
                Clone.Parent = model.SlimeStorage
                Clone.Transparency = 0
                Clone.Position = Vector3.new(Slimes.StartPos.Position.X,Y,X) -- change 23 to wherever you have and flip around the X and slimes start position depending on orientation and change the X to a Z
                X += Slimes.StartPos.Size.Z + distanceBetweenSlimes
            end
        else
            repeat 
                counterDraw -= 1
            until slimeList[counterDraw] ~= nil or counterDraw == 0
            counterDraw += 1
        end
        counterDraw -= 1
    end
end

Kinda fixed it but I got a different issue now it times out and gives the error 12:06:54.778 Workspace.Slime thing.Script:40: Script timeout: exhausted allowed execution time - Server

spare grotto
#

the repeats need a wait, is liek a bucle so it needs a wait to dont crash

upbeat sequoia
#

why is it all nil when we are adding one to slime1 in the table?