Why does my script only work in studio?

Hello, I have a code that procedurally generates rooms as you walk along.
But for some reason, it only works in studio?

Here are some videos

Studio

Actual Game

https://www.roblox.com/games/7246027585/walk-through-doors-forever-simulator?
it wouldn’t let me upload a video for some reason so I just linked the game

here is the code:

local visited = {}
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		while char.Humanoid.Health > 0 do
			wait(0.1)
			for i = -25,1 do
				local zPos = math.floor((char.HumanoidRootPart.Position.Z + i * 100) / 100 + 0.5) * 20
				if visited[zPos] == nil then
					visited[zPos] = script.Room:Clone()
					local p = visited[zPos]

					p:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,1.5,zPos)))

					p.Parent = workspace.Room
				end
			end
		end
	end)
end)

any help is appreciated, thank you in advance

Can you send the output from the Server side of the actual game?

i don’t know what this means

I couldn’t figure out the cause however, there are a few thing you might want to change

  • do not use a while loop! This blocks the entire script preventing further code Freon running. Use either event system for a heartbeat event

  • your code doesn’t take into account multiple users. Create a visited array for each user

Script timeout ususally means there is a infinite loop somewhere. It happen when the game tries to do the same thing over and over infinite times. Try adding wait()

how can i fix this? I am very new to scripting and I made this sort of as practice I guess.

Can you send the rest of your code?

It’s probably because PlayerAdded sometimes doesn’t execute in time depending on the script location.

local Players = game:GetService("Players")

local visited = {}

local function OnCharacterAdded(char)
	while char.Humanoid.Health > 0 do
		wait(0.1)
		for i = -25,1 do
			local zPos = math.floor((char.HumanoidRootPart.Position.Z + i * 100) / 100 + 0.5) * 20
			if visited[zPos] == nil then
				visited[zPos] = script.Room:Clone()
				local p = visited[zPos]

				p:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,1.5,zPos)))

				p.Parent = workspace.Room
			end
		end
	end
end

local function OnPlayerAdded(plr)
	if plr.Character then
		OnCharacterAdded(plr.Character)
	end
	plr.CharacterAdded:Connect(OnCharacterAdded)
end

for _, Player in ipairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Make sure you always use GetService because if none of the ServerScripts uses GetService it won’t work because the service needs to be created/loaded first.

1 Like

You could use a heartbeat event instead of a while loop

local rs = game:GetService(“RunService”)

function onHeartbeat() {
//code to check player position on load rooms
}

rs.heartbeat:Connect(onHeartbeat)

image

this happened with your code

I forgot to add :Connect() to CharacterAdded, it should if you copy and paste again

image

same thing

perhaps you forgot to publish?

i don’t understand how i’d incorporate this, all it gives me in the code area are red lines

no, i didn’t
same thing happens

works fine for me
image

local Players = game:GetService("Players")

local visited = {}

local function OnCharacterAdded(char)
	print("hey")
end

local function OnPlayerAdded(plr)
	if plr.Character then
		OnCharacterAdded(plr.Character)
	end
	plr.CharacterAdded:Connect(OnCharacterAdded)
end

for _, Player in ipairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Sometimes it doesn’t publish even if you clicked it, try publish as then select the same place.

image

Are you sure you updated the code? Because the error is the same as before I edited the code… Can you copy and paste the exact code you’re using and send it here?

Your getting those errors because of the while loop you have in the onCharacterAdded().

When a while loop is in the code, the server stops doing everything else and workes on that part only. This continues until the loop ends. The code you have run infinitely so the server is returning an error

Not really, it would be a problem if the while-loop were outside the local function. Functions only execute when they are called, so the loop is technically not running so it cannot yield the thread.