Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

3 comments

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.

0

Secondly, for loops aren't restricted to looping through tables.

0

Alright, my mistake. Thanks for the information.

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

2 comments

0

Thank you!

1

-1 for having no explanation whatsoever.

This discussion is preserved as it was. New answers, comments, and votes are closed.