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

Teleporting a player to a random place in the map by a gui?

Asked by 5 years ago

I need to(in my game) teleport a player to a random part in the map I've created. I'm using the table method of where you have the map, and then you have the "SpawnPoints" model inside said Map. From a GUI I've made I have a Text-Button, and there's a script inside said Text-Button which should teleport the player to a random spawnpoint in the "SpawnPoints" model" --This is the script I'm using

local Workspace = game:GetService("Workspace") local SpawnPoints = Workspace.Map.SpawnPoints:GetChildren() local ChosenSpawnPoint = SpawnPoints[math.random(1,#SpawnPoints)]

script.Parent.MouseButton1Click:Connect(function(player) local Humanoid = player.Humaoid

if Humanoid.Parent:FindFirstChild("Torso") then
    Torso.CFrame = CFrame.new(ChosenSpawnPoint.Position + Vector3.new(0,3,0))

elseif Humanoid.Parent:FindFirstChild("UpperTorso") then
    UpperTorso.CFrame = CFrame.new(ChosenSpawnPoint.Position + Vector3.new(0,3,0))      
end
wait(1)
script.Parent.Parent.Parent:Destroy()

end)

If you can help me, I'd be grateful :)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You don't use vector values in a CFrame line, instead you use CFrame, when updating a block's position with Vector values, you use:

Humanoid.Parent:FindFirstChild("UpperTorso").Position = Vector3.new(0,3,0)

Also make sure to include the way you receive these values, I'm assuming you have done: (You also spelt Humanoid wrong).

local Humanoid = player.Character.Humanoid
local SpawnPoints = game.Workspace.SpawnPoints:GetChildren()
ChosenSpawnPoint = SpawnPoints[math.random(1,#SpawnPoints)]

so the script knows what the humanoid is and where it is. So the corrections are:

if Humanoid.Parent:FindFirstChild("Torso") then
    Humanoid.Parent:FindFirstChild("Torso").CFrame = ChosenSpawnPoint.CFrame + CFrame.new(0,3,0)

elseif Humanoid.Parent:FindFirstChild("UpperTorso") then
    Humanoid.Parent:FindFirstChild("UpperTorso").CFrame = ChosenSpawnPoint.CFrame + CFrame.new(0,3,0) 
end
wait(1)
script.Parent.Parent.Parent:Destroy()
Ad

Answer this question