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)
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()
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.
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.
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.