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. ;/
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)
More info on...
If you need anymore information, feel free to comment below. I will do my best to help.
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:
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:
Hope it helped :)