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

How to teleport a monster to a player 10 studs behind?

Asked by 5 years ago
Edited 5 years ago

The random PlayerPicker() function works but the TpToPlayer doesn't tp the monster to the player but to the centre of the map

01local Players = game.Players:GetPlayers()
02local nplrs = #Players
03local Randomplayer = nil
04local MonsterTorso = script.Parent.Torso
05 
06 
07local function PlayerPicker()
08    if nplrs > 0 then
09        Randomplayer = Players[math.random(1, nplrs)]
10    end
11 
12end
13 
14local function TpToPlayer()
15    MonsterTorso.CFrame = CFrame.new(Vector3.new(10, Randomplayer, Randomplayer))
16end
0
10 studs in what direction? infront behind User#5423 17 — 5y
0
Behind YazaTheGreat 5 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

The main issue is that Randomplayer is the Player object whcih is not the Players position. As you are getting a list of player at the start of the script it may also cause issue if a player leaves the game.

To avoid this I would put it all in the PickPlayer function

01local plrServ = game:GetService('Players')
02 
03local function PlayerPicker()
04    local Players = plrServ:GetPlayers()
05 
06    if #Players  > 0 then
07        local RandomPlayer = Players[math.random(1, #Players)]
08        if RandomPlayer.Character then -- character will be nil if they have not spawned
09            RandomPlayer.Character.Torso.CFrame-- return the player position
10        end
11    end
12    -- you need to handle if there are no players do you return a def CFrame ?
13    -- or if the player has no character
14end

Now that you have the players position we can work out what is "back" from the charactes CFrame. We can work in object space (this is outside the scope of this question please reaserch this if you do not know what it is)

1local function TpToPlayer()
2    local teleportTo =  PlayerPicker() * CFrame.new(0,0, -10) -- move back 10 studs
3    MonsterTorso.CFrame = teleportTo
4end

You may find that using CFrame will teleport the monster into object in which case you would want to use Position as it will then teleport the monster on top of object.

I hope this helps. Please comment if you have any other questions about this post.

0
Line 2 on the TpToPlayer() function doesn't work and i receive this error: bad argument #1 to '?' (CFrame expected, got nil) YazaTheGreat 5 — 5y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try this:

01local Players = game.Players:GetPlayers()
02local nplrs = #Players
03local Randomplayer = Players#nplrs
04local MonsterTorso = script.Parent.Torso
05 
06 
07local function PlayerPicker()
08    if nplrs > 0 then
09        Randomplayer = Players[math.random(1, nplrs)]
10    end
11 
12end
13 
14local function TpToPlayer()
15    MonsterTorso.CFrame = CFrame.new(Vector3.new(10, Randomplayer, Randomplayer))
16end
17TpToPlayer()
0
the code you just returned is the same as what i posted YazaTheGreat 5 — 5y

Answer this question