Why does my script only work in studio?

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.

I don’t think that’s the case. Function run on the same thread as the main script.
Take a case like this

function waitTenSec()
wait(10)
end

local start = tick()
waitTenSec()
waitTenSec()
print(tick()-start)

This will print out 20

yes, i am using your code

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)

try changing

local function OnCharacterAdded(char)
//your code
end

To

local OnCharacterAdded = coroutine.wrap(function (char)
//your code
end)

make sure to change//your code with what you have right now

same thing happens, this is getting really frustrating

great now it doesn’t work in studio either

I responded to your other topic.

Response: