Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

infinitely generated rooms, works in studio but not in-game. why is this?

Asked by
Foxxive 10
3 years ago
Edited 3 years ago

hi, i have a code that creates a new room as you walk along, the point of the game is just to open doors infinitely.

this is my code:

01local Players = game:GetService("Players")
02 
03local visited = {}
04 
05local function OnCharacterAdded(char)
06    while char.Humanoid.Health > 0 do
07        wait(0.1)
08        for i = -25,1 do
09            local zPos = math.floor((char.HumanoidRootPart.Position.Z + i * 100) / 100 + 0.5) * 20
10            if visited[zPos] == nil then
11                visited[zPos] = script.Room:Clone()
12                local p = visited[zPos]
13 
14                p:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,1.5,zPos)))
15 
View all 33 lines...

it only works in studio for some reason, and in-game it gives me errors along the lines of "execution time exhausted" and I have no idea how to fix it.

i've made several posts on the dev forums and got no help, so I'm posting here in hopes of getting an actual answer that helps

thank you

1 answer

Log in to vote
1
Answered by 3 years ago

"Execution time exhausted" is an error that you get when you forgot to put a wait() (wait 1 frame) on a loop. Lua is a language that runs very fast, to fix it replace all your code with this or just straight up put a wait() at the end of every loop:

01local Players = game:GetService("Players")
02 
03    local visited = {}
04 
05    local function OnCharacterAdded(char)
06        while char.Humanoid.Health > 0 do
07            wait(0.1)
08            for i = -25,1 do
09                local zPos = math.floor((char.HumanoidRootPart.Position.Z + i * 100) / 100 + 0.5) * 20
10                if visited[zPos] == nil then
11                    visited[zPos] = script.Room:Clone()
12                    local p = visited[zPos]
13 
14                    p:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,1.5,zPos)))
15 
View all 36 lines...

It's basically the same code however all the loops have a wait() inside.

Ad

Answer this question