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

Could anyone please help me fix this?

local player = game.Players:GetChildren()
playersingame = 0

function onPlayerEntered(nP)
playersingame = playersingame +1
end

game.Players.ChildAdded:connect(onPlayerEntered)

while playersingame > 0 do
x = Instance.new("Hint",Workspace)
x.Name = "GameStart"
x.Text = "New game is starting."
wait(1)
x.Text = "New game is starting.."
wait(1)
x.Text = "New game is starting..."
wait(1)
x.Text = "Game in progress..."
player[i].Character.MoveTo(Vector3.new(0.6, 0.59, 3.8))
end

It's supposed to create a Hint that tells you the game status, then, it's supposed to teleport all players to this position "0.6, 0.59, 3.8", yet it doesn't do any of that.

1 comment

0

Sorry, I'm not too good at scripting.

2 answers

✓ Accepted answer

Your problem is player[i] does not work unless you have a variable named i, like in a for loop. Second, MoveTo() is a method, which means you much use :.Therefore, you can easily do this:

for i = 1,#player do
	player[i].Character:MoveTo(Vector3.new(0.6, 0.59, 3.8))
end

1 comment

0

Thanks!

Please tab and space your code properly!


Your definition of player (should be players probably since it is a list) should happen immediately before doing the loop!

Otherwise there could be people who joined or left in the interim!


Also, your while loop should probably always run, rather than have the condition that only run while there are players. If there were not players, then the loop would stop and never start again!

Instead we just check each loop that there are players.


In addition, you should check that the players have both a Character!

Otherwise your script could error if someone is dead and it would stop working!


Also, MoveTo is a method, so you use : instead of . on it.


All of this is in addition to Perci1's addition of the for loop

while true do
	wait(1);
	local players = game.Players:GetChildren()
	if #players > 0 then
		x = Instance.new("Hint",Workspace)
		x.Name = "GameStart"
		x.Text = "New game is starting."
		wait(1)
		x.Text = "New game is starting.."
		wait(1)
		x.Text = "New game is starting..."
		wait(1)
		x.Text = "Game in progress..."
		for i = 1, #players do
			if players[i].Character then
				players[i].Character:MoveTo(Vector3.new(0.6, 0.59, 3.8))
			end
		end
	end
end

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