Script causing server delay

Greetings fellow developers! I’m here needing a little help.

  1. What do you want to achieve? Keep it simple and clear!

I wish to relieve the server of delay/lag when running this line of code.

  1. What is the issue? Include screenshots / videos if possible!

When this code is cloned into the workspace and then runs, everything gets slow and the server is slow to respond.

for i, v in pairs(game.Players:GetPlayers()) do
wait()
if v.Character:FindFirstChild(“Humanoid”) then
v.Character.Humanoid.Sit = false
wait()
if v.Character:FindFirstChild(“HumanoidRootPart”) then
v.Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.TPFolder.Obby1.Position)
wait()
end
end end

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

As you can see, I have inserted “wait()” commands for each index, but the issue still remains

I appreciate any help/feedback, and if you have a better way to teleport all players, please let me know.

Thanks everyone!

Not Really Sure how it slows your game, But try this:

local function SetPosition(Location)
for _,Player in pairs(game.Players:GetPlayers()) do
Player.Character:WaitForChild("Humanoid").Sit = false
Player.Character:MoveTo(Location) -- Moves Character without the Need of the PrimaryPart
end
end

SetPosition(workspace.TPFolder.Obby1.Position + Vector3.new(0,2,0) -- So the Player doesnt spawn inside the part.

You Dont need to add wait.


This is a good tip.

True.


Note: If you want to put your code in a Script format, use ```

1 Like

Cant really see anything in the code that can make the game lag. Are you constantly cloning it into the workspace? if so then yes thatll cause lag because its looping through all the players.

Also tip: Dont place scripts inside the Workspace. Place them into the “ServerScriptService”

Also one more small tip:

  1. Use “ipairs()” for arrays. ipairs() will loop in order meanwhile pairs() doesnt.

  2. You dont need to do “Root.CFrame = CFrame()” you can just do “Character:PivotTo(CFrame)” to move the player :slight_smile:

2 Likes

Not the solution probably but a quick tip.
Do NOT use wait() anymore - start using task.wait()!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.