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

How do I make someone spawn at a random part?

Asked by 7 years ago

Ive been working on a script to make a player teleport to a random point on a map that I have marked with little Spawnlocation parts (not the Spawnlocation its self, but a part) around the map. But when I press "Teleport to Arena" on the GUI it takes me to the same one. How do I make it randomly pick which one to goto?

Player = game.Players.LocalPlayer
Spawn = game.Workspace.SpawnLocation
BlackScreen = script.Parent.BlackScreen
TeleportButton = script.Parent.TeleportButton
Status = script.Parent.BlackScreen.Status
HasPressedTeleport = false

function MouseEnterTeleportButton()
    TeleportButton.FontSize = "Size36"
    TeleportButton.BackgroundColor3 = Color3.new(255, 1, 1)

end

function MouseLeftTeleportButton()
    TeleportButton.FontSize = "Size24"
    TeleportButton.BackgroundColor3 = Color3.new(1, 1, 1)

end

function TeleportPlayer()
    if HasPressedTeleport == false then
        BlackScreen.Transparency = 1
        wait(0.1)
        BlackScreen.Transparency = 0.9
        wait(0.1)
        BlackScreen.Transparency = 0.8
        wait(0.1)
        BlackScreen.Transparency = 0.7
        wait(0.1)
        BlackScreen.Transparency = 0.6
        wait(0.1)
        BlackScreen.Transparency = 0.5
        wait(0.1)
        BlackScreen.Transparency = 0.4
        wait(0.1)
        BlackScreen.Transparency = 0.3
        wait(0.1)
        BlackScreen.Transparency = 0.2
        wait(0.1)
        BlackScreen.Transparency = 0.1
        wait(0.1)
        BlackScreen.Transparency = 0
        wait(0.5)
        Player.Character:MoveTo(Spawn.Position)
        wait(0.5)
        Status.Transparency = 1
        wait(0.1)
        BlackScreen.Transparency = 0
        wait(0.1)
        BlackScreen.Transparency = 0.1
        wait(0.1)
        BlackScreen.Transparency = 0.2
        wait(0.1)
        BlackScreen.Transparency = 0.3
        wait(0.1)
        BlackScreen.Transparency = 0.4
        wait(0.1)
        BlackScreen.Transparency = 0.5
        wait(0.1)
        BlackScreen.Transparency = 0.6
        wait(0.1)
        BlackScreen.Transparency = 0.7
        wait(0.1)
        BlackScreen.Transparency = 0.8
        wait(0.1)
        BlackScreen.Transparency = 0.9
        wait(0.1)
        BlackScreen.Transparency = 1
        wait(0.1)

    end
end

TeleportButton.MouseEnter:connect(MouseEnterTeleportButton)
TeleportButton.MouseLeave:connect(MouseLeftTeleportButton)
TeleportButton.MouseButton1Down:connect(TeleportPlayer)

1 answer

Log in to vote
0
Answered by 7 years ago

Try this code:

Player = game.Players.LocalPlayer
BlackScreen = script.Parent.BlackScreen
TeleportButton = script.Parent.TeleportButton
Status = script.Parent.BlackScreen.Status
HasPressedTeleport = false

function MouseEnterTeleportButton()
    TeleportButton.FontSize = "Size36"
    TeleportButton.BackgroundColor3 = Color3.new(255, 1, 1)

end

function MouseLeftTeleportButton()
    TeleportButton.FontSize = "Size24"
    TeleportButton.BackgroundColor3 = Color3.new(1, 1, 1)
end

local SpawnTable = {}
for i,v in pairs(workspace:GetChildren()) do
    if v.Name == "SpawnLocation" then
        table.insert(SpawnTable, v)
    end
end

function GetRandomSpawn()
    return SpawnTable[math.random(#SpawnTable)]
end

function TeleportPlayer()
    if HasPressedTeleport == false then
    for i = 1,0,-0.1 do
        BlackScreen.Transparency = i
        wait(0.1)
    end
        wait(0.5)
        Player.Character:MoveTo(GetRandomSpawn().Position)
        wait(0.5)
        Status.Transparency = 1
        wait(0.1)
    for i = 0,1,0.1 do
        BlackScreen.Transparency = i
        wait(0.1)
    end
    end
end

TeleportButton.MouseEnter:connect(MouseEnterTeleportButton)
TeleportButton.MouseLeave:connect(MouseLeftTeleportButton)
TeleportButton.MouseButton1Down:connect(TeleportPlayer)

Leave an upvote if I helped! :)

Ad

Answer this question