For example, if i will run a script that teleports every single player to a part in the workspace called TELEPORTPART, and the script also changes their speed to 30
My question is: to avoid errors like the script trying to use a character of a player that just left the game, i need to check for each parts existances?
Something Like:
for i,player in pairs(game.Players:GetPlayers) do
if player and player.Character and player.Character.HumanoidRootPart and player.Character.Humanoid then
player.Character.HumanoidRootPart.CFrame = workspace.TELEPORTPART.CFrame
player.Character.Humanoid.WalkSpeed = 30
end
end
Yeah, like @Doomcolp said, you most likely only need those. You don’t even need to check if the player exists either, because the table would include it if it did exist.
You should also be using :PivotTo instead of setting the primary part’s CFrame.
Code:
local Players = game:GetService("Players")
for i, player in Players:GetPlayers() do
local character = player.Character
if character then
character:PivotTo(workspace.TELEPORTPART.CFrame)
character.Humanoid.WalkSpeed = 30
end
end