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

Reliable way to wait for players in a function?

This question sounds weird and possibly even incredibly obvious-- but let me explain.

This script here, works in studio-- but as you know Roblox actually takes time to load the things I need in a server-- which creates the problem

local function RandomPlayer()
	local players = game.Players:GetPlayers()
	if #players > 0 then
		return players[math.random(#players)]
	end
end	

local FacePlayer = coroutine.wrap(function()
--	wait(5)	
	
	while wait(0.1) do
	
	local Plr = RandomPlayer()
		
	local Chr = Plr.Character
	
	local TorsoPos = Chr:WaitForChild("Torso").Position	
	
	local Gyro = Boss.Core.BodyGyro
	Gyro.CFrame = CFrame.new(Boss.Core.Position, TorsoPos) 
	Gyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	Gyro.P = 999999
	end
end)

FacePlayer()

The problem is in the RandomPlayer() function-- I don't know how to write a check to make sure there are players to randomly choose as Roblox loads everything-- without having to just stop the script for 5 seconds or having ALL MY ATTEMPTS OF FIXING IT JUST TIME OUT AND STOP THE SCRIPT ANYWAY! >;0

Also this has a fine habit of breaking if your torso doesn't exist because you say-- fell off the map Ou0; Something that I'm also having a hard time writing a check for.

Yes, I know there are other things wrong with this-- but those problems I want to solve myself. For now I'd like help with actually waiting for players ;o;

3 comments

0

Well first off, it should be 'return players[math.random(#players, 1)]'

1

It doesn't matter.

1

I don't need the 2nd arg for this :P

1 answer

✓ Accepted answer

Choosing players randomly may be unfair in games.You can put an array and put player names into it to create a line as players gets in a server(This may sound like a weird idea but Death Run 3 uses it, it's pretty good :) ) So there won't be problem of waiting for players. Players will be already there because they also will be in array. (Do not forget to delete players from array when they leave) If you still want to make it random, still try to put all players in an array and choose randomly from that array, so it will be random and problemless of waiting. Here's a coding example of what I've said:

PlayersArray = {}
CountArray = {}
game.Players.PlayerAdded:connect(function(Player)
	PlayersArray[#PlayersArray+1] = Player
	CountArray[Player.Name] = #PlayersArray
end)
game.Players.PlayerLeaving:connect(function(Player)
	PlayersArray[CountArray[Player.Name]] = nil
end)

function RandomPlayer()
	done = false

	a = PlayersArray[math.Random(#PlayersArray)]
	if a == nil then
		done = false
	else
		done = true
	end
	if done == true then
		return a
	else
		RP = RandomPlayer()
		return RP
	end
end

Also you can use

Player:LoadCharacter()

just before looking for torso, so there won't be a problem about torso.

6 comments

0

Making a list with nils in it is a very bad idea. Use table.remove .

0

game.Players:GetPlayers() is the list of all players. There's no reason to make your own list.

0

Well, at first it didn't work because the table was still empty until my character finished loading-- so I just waited until the first item in the table wasn't nil. I can't seem to do the same thing for :GetPlayers() so, I guess it works ;o;

1

eue I just got everything to work properly. It now retargets when a player leaves, doesnt break with a missing torso and it looks smooth~ Thanks, I learned alot!

0

No problem :) Have fun.

0

@BlueTaslem That's the point. You can get a table by using :GetPlayers(), but it does not work properly at all times. Also, table.remove would be nice but his goal was to make sure that he won't get any errors, and you cannot trust functions Roblox provides when you want to make sure you do not want to get any error.

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