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

I have code that moves a player to a certain block. It reads the code but doesn't move them, why?

I am working with tables and I am creating a table of 4 blocks in a model. I create this table and do a math.random from 1 to the amount of players in game. After I do so I create a for i,v in pairs do loop. I check for the players and have them teleport to the selected brick in a table, but it does not teleport the player. Instead it reads over it and moves to the next line. I tried using CFrame, Vector3, :MoveTo() and none of them worked.

game.Players.PlayerAdded:Connect(function(Person)
    Person.CharacterAdded:wait()

	local Players = game.Players:GetPlayers() --Gets Players
	local Table = {Players}

	local Random = math.random(1, #Players) --Finds Amount of Players
	print(Players[Random])
	
	local PickedPlayer = Players[Random]       --Picks Player  
	
	local ModelTest = game.Workspace.TestModel2:GetChildren() --(4 Parts)
	
	if Random == 1 then
		for i, v in pairs(ModelTest) do
			if v == ModelTest[1] then
				PickedPlayer.Character:MoveTo(ModelTest[1].Position)
				print("Moved Past")
				PickedPlayer.Character.Torso.Transparency = 0.9
			end
		end
	end
end)

It prints "Moved Past" ignoring the previous line and moving onto the next making the Torso's transparency 0.5.

1 answer

✓ Accepted answer

There is actually no problem with your script, this happens because the character you've chosen hasn't loaded yet (attempted at the beginning of the script which targets everybody that joins) I've added a repeat loop until the character is not nil.

game.Players.PlayerAdded:connect(function(Person)
    Person.CharacterAdded:connect(function(char)

    local Players = game.Players:GetPlayers() --Gets Players
    local Table = {Players}

    local Random = math.random(1, #Players) --Finds Amount of Players
    print(Players[Random])

    local PickedPlayer = Players[Random]       --Picks Player  

    local ModelTest = game.Workspace.TestModel2:GetChildren() --(4 Parts)

    if Random == 1 then
        for i, v in pairs(ModelTest) do
            if v == ModelTest[1] then
				repeat wait() until PickedPlayer.Character 
                PickedPlayer.Character:MoveTo(ModelTest[1].Position)
                print("Moved Past")
                PickedPlayer.Character.Torso.Transparency = 0.9
            end
        end
    end
end)
end)

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