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

Could anyone please help me fix this?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
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.

0
Sorry, I'm not too good at scripting. Awesomepizzaman5 5 — 10y

2 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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
0
Thanks! Awesomepizzaman5 5 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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

Answer this question