Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can't LoadCharacter() at specific interval, why?

Asked by 6 years ago

I'm unable to get my script to load all the players in the server. I'm pretty sure it's because I'm not defining the players right. Can someone help me define the players?

Code:

player = game.players
while true do 
    wait(60)
    player:LoadCharacter()

end

2 answers

Log in to vote
4
Answered by
lunatic5 409 Moderation Voter
6 years ago

There are a few things wrong with this code. First of all, "players" does not exist. It's "Players". As a matter of fact, it is recommended to use :GetService as well. Also, use local variables (For example: "local x = 5"). You also cannot load the character of the Players service, as it is not an actual player instance. To combat this, you should loop through all of the player's in Players by using a for loop. However, you can only loop through tables, so you should use :GetPlayers as it returns a table. In a scenario in which you aren't looping through the players in Players or the players in a team, you would use :GetChildren instead, as it also returns a table.

Here is the final product:

local players = game:GetService("Players")

while true do 
    wait(60)
    for _, player in pairs(players:GetPlayers()) do
        player:LoadCharacter()
    end
end
1
Using `:GetService()` is really not necessary unless you are writing code for the client. Outside of the client, it is not recommended to use one over the other; it's up to user preference. Client-side code should use `:GetService()` because a hacker can rename services to whatever they like, and in the process cause badly written scripts to error. Link150 1355 — 6y
0
Secondly, `for` loops aren't restricted to looping through tables. Link150 1355 — 6y
0
Alright, my mistake. Thanks for the information. lunatic5 409 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

I'll fix up the code for you:

local Players = game.Players
while true do
    wait(60)
    for i,v in pairs(Players:GetPlayers()) do
        v:LoadCharacter()
    end
end
0
Thank you! Michael_TheCreator 166 — 6y
1
-1 for having no explanation whatsoever. Link150 1355 — 6y

Answer this question