How do I teleport a random player to a certain spot every certain amount of time?

Hi! I’m currently trying to make a simon says type game, so I’ve been trying to make a script that teleports a random player to a certain spot every certain amount of time.
I only know the basics of scripting, and since I couldn’t find a tutorial on it and I had no idea where to start, I asked the code A.I (which is still learning). Only problem is it doesn’t work and I can’t find where it went wrong.

local function getRandomPlayer()
	local players = game.Players:GetPlayers()
	local randomIndex = math.random(1, #players)
	return players[randomIndex]
end

local function teleportRandomPlayer()
	local player = getRandomPlayer()
	if player then
		local destination = Vector3.new(-11, 81, 51)  
		player.Character.HumanoidRootPart.CFrame = CFrame.new(destination)
	end
end


while true do
	wait(30)
	teleportRandomPlayer()
end

It would be great if someone can give advice on how to debug the code, thanks.

2 Likes
while true do
	wait(30)
	teleportRandomPlayer()
end

you can make this into this:

while task.wait(30) do
	teleportRandomPlayer()
end
3 Likes

I know,the player is not the model

Get the player character and do the amendemnt

And also,the player you recieve will not work as it is a vararbilethe player is result,so you probably cannot get the character from it

Also change the local function to function only

2 Likes

i do not know what is going wrong, maybe replace the script with this to make sure its running (debug)

local function getRandomPlayer()
	local players = game.Players:GetPlayers()
	local randomIndex = math.random(1, #players)
    local player = players[randomIndex]
print('got player: ', player)
	return player
end

local function teleportRandomPlayer()
	local player = getRandomPlayer()
	if player then
print('got player')
		local destination = Vector3.new(-11, 81, 51)  
		player.Character.HumanoidRootPart.CFrame = CFrame.new(destination)
print('sent player to location')
	end
end


while task.wait(30) do
	teleportRandomPlayer()
        print('30 seconds waited and function fired')
end
2 Likes

uh, if a function returns and you call the function to give a var a value, it will set the var to the returned value

and also, if you have the player you can always get the character (if loaded)

2 Likes

this is useless, as it wont change anything

if you already have the player (instance) you can find the character

2 Likes

U print player.Character and see if it works

2 Likes

That was a good idea and it appears that it got the player twice, but it says that the HumanoidRootPart is not a valid member of my character.

2 Likes

maybe you can try to teleport the torso, i don’t understand how it cannot find the humanoidrootpart.

(by doing this):

3 Likes

Character is probably nil(if i am not wrongl)

2 Likes

Not really,it only get the result of the player,but you cannot use that player result to obtain the character

2 Likes

sorry man, you can, read up on your docs down below.

player.Character is a real thing, here you go.

2 Likes

U print the player.character,see if u can get anything(the print should be a username

2 Likes

game.Players:GetPlayers() is better due to the fact of you getting the players, just incase of a non player object being in the player service

edit: :GetPlayers() gets every player instance.

edit2 because im not bumping:

there is, but using getplayers is better for getting every player

2 Likes

Solution is there.look at it,i believe that is the reason

1 Like

Although I’m not quite sure what situation I would have to be in to know how many players are in the game and the players that are not, but I might find out later!

1 Like

Answers are in dev forum,search them.
GetPlayers: Usage()
– when doing a pvp system
– when requiring the coins and not the character

1 Like

Use :PivotTo() instead of manually editing the CFrame of HumanoidRootPart.

Example:

player.Character:PivotTo(targetPosition);
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.