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

How would I transport a player to one of the 10 parts I have named a certain name?

Asked by 6 years ago

I'm fairly new to scripting, and I learn of sites like this, but I am having a bit of trouble scripting this.I just need a jump start to it.

So I have a map and a lobby. In the lobby, I have the spawns that ROBLOX give you when you press the spawn button. In the map, I have multiple parts Labelled "Spawn1", "Spawn2", "Spawn3" etc.

So I want to script a text button, when pressed it will teleport you one of the random parts spread out across the map.

So I'm guessing it will be a local script inside the text label then it will be

local button = script.Parent
local mapspawns = 

after "mapspawns = ", I'm not sure how I would define the multiple parts, so how would I do that bit?

Next, I want to teleport the players, I'm not too sure on this bit either. I would guess you move the Torso, because that's the main part. (correct me if it's wrong) So can someone teach me how to teleport the player, and not get them killed when doing so?

So I want the script to pick out a random spawn part, so I would put my bets on saying that a table would be required,(I'm awful at tables), and the math.Random() function. I looked it up on the developer blog Roblox has and it came up with generating random numbers.

So I'm guessing this is one way that it can be done: So i define Spawn1 as "1", Spawn2 as "2" etc, then I make math.Random() pick a number between 1 and 10 (let's say I have 10 spawns) Then here I want to make the picked number to have a link to the Spawns. So if I got 3 from the generation, I want the script to know I'm on about Spawn3. Do I have to do something here to let it know, likemath.Random().Value or something (does that even exist?) Then I want the teleportation bit to go here, so teleport the player's Torso to the value of math.Random()'s assigned Spawn. (Spawn1 = 1, etc,)

later on in the script, I can make the button go invisible after I press it and all myself. Simply by doing

button.Visible = false -- I defined it above

I know this is a huge question, but hopefully it will teach me the basics of a few things, which in turn can help me to script in the future by my self.

Thanks for reading! Please can you help me out?

EDIT: I just had an epiphany. So can I define the variable SpawnNumber using math.random() so for example

local SpawnNumber = math.Random(1,10)

then can I do this?

if SpawnNumber.Value == 3
    then 
        Teleport function here

Can someone tell me if this is right? Or Help me out if I have gone wrong and can someone teach me how I can do the things stated above?

Not sure what I should do after here though. ;/

0
I will be answering the entirety of your question in a bit. User#25115 0 — 6y
0
Ok thanks for your time and effort. tonyv537 95 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Hello there, I am glad to see that you are already on the path to success. I am only here to give you the little push that will allow you to create this program. First of all, because this is a text button that you are scripting, I recommend that you use a LocalScript. All GUI manipulation should take place on the client (with LocalScripts). I am going to assume that you will place this LocalScript into the button. All the things we need for this to work are the player, the button, and a random number. The player can be found by using the LocalPlayer property of the Players service.


Here is an example script of what we have so far:

local players = game:GetService("Players") 
--[[ 
    This is the recommended way to get 
    the players service, especially 
    from a LocalScript.
--]]
local player = players.LocalPlayer 
-- getting the player with the LocalPlayer property
local button = script.Parent 
-- assuming that the script is a child of the button
--[[
    Here we will add the clicked event
    so that when a player clicks on the
    button, it will call the connected function.
--]]
button.MouseButton1Click:Connect(function()
    local randomNumber = math.random(10) 
    --[[ 
        if you use one argument, such as ten, 
        it assumes that you are starting at 1.
    --]]
end)

Now comes the simple element of finding the part, and teleporting to it. I am going to assume that all of the parts are in the Workspace. Roblox provides a nice little method for finding certain parts by their name called Instance:FindFirstChild. Because you have named all of your parts the same thing except for the number, it is a simple matter to find a random part via concatenation.


Using all of the elements I have mentioned here, let us put together our script for getting the random part (adding on to our previous script):

-- assuming the rest of the script is up here
button.MouseButton1Click:Connect(function()
    local randomNumber = math.random(10) 
    local randomPart = game.Workspace:FindFirstChild("Spawn"..randomNumber)
    --[[
        So if random number were 3,
        randomPart would be the part
        in the workspace with the name 
        of Spawn3. This is possible because
        of the ".." concatenation operator.
    --]]
end)

The only thing left to add, is the actual teleportation of the player. For this, we will use the HumanoidRootPart of the player's character. To teleport, we need only change the position of the HumanoidRootPart to that of the brick, plus a few y studs of offset.


Final script:

local players = game:GetService("Players") 
local player = players.LocalPlayer 
local button = script.Parent 
button.MouseButton1Click:Connect(function()
    local randomNumber = math.random(10) 
    local randomPart = game.Workspace:FindFirstChild("Spawn"..randomNumber)
    local humanoidRootPart = player.Character.HumanoidRootPart
    --[[ 
        we don't need to wait for the character,
        because players cannot click if they are 
        not loaded in. 
    --]]
    humanoidRootPart.CFrame = CFrame.new(randomPart.Position.X, randomPart.Position.Y + 5, randomPart.Position.Z) 
    --[[
        CFrames are the recommended 
        method for teleportation.
        I added an offset of five, you can 
        change that if you wish
    --]]
end)

I hope this helps. Have a great day scripting!

More info on...

If you need anymore information, feel free to comment below. I will do my best to help.

0
Good answer, but have so much comments. yHasteeD 1819 — 6y
0
Thank you, I don't know how else I would fit the information in. User#25115 0 — 6y
0
create a variable for the character. as in mine to detect the character without returning errors. yHasteeD 1819 — 6y
0
Because I get the humanoidRootPart in the event, I don't need to wait for the character. User#25115 0 — 6y
View all comments (13 more)
0
I did it that way because it was simpler to write out. It also meant less confusion when explaining things. User#25115 0 — 6y
0
But is more safe to wait for the character. yHasteeD 1819 — 6y
0
and you can use "workspace" instead of "game.Workspace" yHasteeD 1819 — 6y
0
I did this the simple way because it is a simple problem. In a more complex example/situation, of course I would wait for the character. However, I don't feel that it is necessary in this situation. User#25115 0 — 6y
0
Oh. I had not seen that. and i believe it is, people who have lag can click the button before their character loads completely yHasteeD 1819 — 6y
0
No problem. User#25115 0 — 6y
0
I'm editing all my answers to remove this deprecated function.I always used it thinking it was not deprecated. yHasteeD 1819 — 6y
0
Odd. The answer in that link I posted was removed. User#25115 0 — 6y
0
@yHasteeD, turns out that answer was incorrect anyway. I talked to another member on the discord. User#25115 0 — 6y
0
workspace is not deprecated. User#25115 0 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The most efficient way to do it would be:

--localscript inside of the button

local players = game:GetService("Players")
local player = players.LocalPlayer
local playergui = player.PlayerGui
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local spawns = game.Workspace:WaitForChild("Spawns") -- folder that holds all the parts

local positions = {} -- table that will hold all the possible positions of the spawn parts

math.randomseed(tick()) -- make it random for each client

for _,v in pairs(spawns:GetChildren()) do
    table.insert(positions, v.Position) -- inserting the positions of the spawn parts into the aforementioned table
end

local debounce = false
playergui.SCREENGUINAME.BUTTONNAME.MouseButton1Click:Connect(function()
    if not debounce then
        debounce = true -- to stop spamming
        local randomPos = positions[math.random(1, #positions)] -- randomly picking 1 value (position) from the table
        character:MoveTo(randomPos) -- moving the character to the chosen position
        print(unpack(positions)) -- just for fun; printing the positions of the spawn parts
        humanoid.Died:Connect(function() -- when the humanoid dies
            debounce = false -- allow the button to be clicked again
        end)
    end 
end)

Of course change "SCRENGUINAME" and "BUTTONNAME" to the actual names you used (which you probably already know). These are all of the methods used, to take a closer look:

MouseButton1Click

debounce

math.randomseed(tick())

MoveTo

Humanoid.Died

in pairs

1
No real explanation = bad answer. User#25115 0 — 6y
0
That's your opinion, plus your script wouldn't even work outside of play solo. In terms of efficiency, this is the most efficient yet. KardashevScale 110 — 6y
0
My script would work "outside of play solo." First of all, play solo now emulates a real server. Second, the player's position replicates to the server, so it would work "outside of play solo" when play solo used to not accurately resemble a real server. User#25115 0 — 6y
0
Not only that, but I have tested it, and it works. Please refrain from talking about things you clearly do not understand. User#25115 0 — 6y
View all comments (8 more)
0
Before you say, "You only tested in studio." I want you to know that I have tested it in a published place, and it works exactly as intended. User#25115 0 — 6y
0
Actually, I wasn't referring to that. I was referring to the fact that you used "script.Parent" but I forgot that it's okay because the button is ultimately a descendant of the PlayerGui, my bad. (I was thinking of it as a part for some reason) But since you are so smart, I've had an error with DataStore, mind taking a look? KardashevScale 110 — 6y
0
Sure, do you have discord? Also I am not smart, just experienced. User#25115 0 — 6y
0
Asked a question on another person's question... QUESTION-CEPTION!!! TheeDeathCaster 2368 — 6y
0
Yes, it's Lazarus_Effect #2929. KardashevScale 110 — 6y
0
@ThreeDeathCaster Gotta do what you gotta do sometimes.. KardashevScale 110 — 6y
0
*TheeDeathCaster I'm not a number, sweety. ;) TheeDeathCaster 2368 — 6y
0
Thabsk for this answer. The reason i didnt accept was because the other onr had mofe detail. But I thin k i will try both scripts and see whoch one i like. tonyv537 95 — 6y
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

Its simple, in math.random on teleport you need to use SpawnsLocations:GetChildren()[math] for get the block.

This literally as said will get everything it has inside a location through a numerical value. you can choose the numerical value and start your function(If you do not understand my explanation tell us the comments. my english is very bad, Or if have something wrong, also tell me in the comments.)

Here is a example:

-- Local Script
local player = game.Players.LocalPlayer -- Get player
local char = player.Character or player.CharacterAdded:Wait() -- Get player character

local SpawnsLocations = game.Workspace.Spawns:GetChildren() -- Add :GetChildren() / Get all in Spawns folder
local value = math.random(1,#SpawnsLocations) -- Get random number with 1 to length of spawns location.
if SpawnsLocations[value] ~= nil then -- Check if spawn is not nil
    print("Spawn found!")
    -- Teleport
    char:WaitForChild("Humanoid") -- For no errors on teleport.
    char:MoveTo(SpawnsLocations[value].Position) -- Move player to selected part
end

Wiki pages:

GetChildren

math.random

Hope it helped :)

0
Thanks. This is one part of what I was looking for! I totally forgot about :GetChildren as an option! Do you know anything about teleportation? tonyv537 95 — 6y
0
Yes. yHasteeD 1819 — 6y
0
now, i added to teleport to the selected part. yHasteeD 1819 — 6y
0
:wait() is deprecated User#23365 30 — 6y
View all comments (2 more)
0
oh, changed yHasteeD 1819 — 6y
0
It turns out that that answer I posted was incorrect. workspace is not deprecated. You can use it if you wish. User#25115 0 — 6y

Answer this question