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.
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
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!