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

1 Like

Are you in team create? if so did you COMMIT your scripts?

no, i am not in a team create instance

Are you using roblox beta features? If so, then it won’t work on roblox itself and only on roblox studio.

i’m not using any, no.

i have them enabled but i’m not incorporating any in this game

local visited = {}
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		 while char:WaitForChild("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)

I added a WaitForChild()

maybe that will fix it

the problem is now solved. Thank you all for helping

1 Like