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

Spawning at a random spawn?

Asked by 7 years ago
Edited 7 years ago
local Spawns = game.Workspace:FindFirstChild(Name).Spawns:GetChildren() -- Get (around 25 random spawns from a folder)
local SpawnLoc = math.random(1,#Spawns) --choose a random spawn 
script.Parent.Parent.Parent.Parent.Parent.Character:MoveTo(SpawnLoc .Position)-- move player to random spawn(etc)

I know I could name each spawn with a number but that's inefficient and I could have 100s of spawns, is there any way that I could do this using math.random? Because the Name and number of spawns change each time so I cant create a table {} or name the spawns, any help? SpawnLoc is returned as a numberValue, when it needs to be an object from the "Spawns" variable ~Thx, Bubs

0
I don't understand. Your code looks fine to me other than you not putting an actual name in the FindFirstChild call. Is there any error with your script or a reason why this method will not work? I probably just missed something. User#11440 120 — 7y
0
well, math.random returns a number for a start, and all the spawns are called "Spawn" and all i need it do to is to spawn to a random one of those, but the SpawnLoc is a number, idk if it needs to be a string or something Bubbles5610 217 — 7y
0
I see. I got you. User#11440 120 — 7y

1 answer

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

You can pick a index from the table to get a random spawn. Since we already have a random number, we can plug that into the index of the table to get a random spawn.

local Spawns = game.Workspace:FindFirstChild("Put Correct Name Here").Spawns:GetChildren()
local SpawnLoc = Spawns[math.random(1,#Spawns)] 
script.Parent.Parent.Parent.Parent.Parent.Character:MoveTo(SpawnLoc .Position)
If you need more of an explanation, please just ask.

Good Luck!

If I helped, please don't forget to accept my answer.
0
Ah this worked thanks! i think i understand it, but whats it called when you put the [] in there? and dont worry i will put the correct name haha, thanks man :) Bubbles5610 217 — 7y
0
When you have a table of values, one way of getting the value you want it by getting the index. Meaning if we had a table = {"Hello","Loveyou"} and we called it print(table[1]) it would print Hello, and if we did, print(table[2]) we would get Loveyou. User#11440 120 — 7y
0
ScriptGuider does a great job at explaining tables here, https://scriptinghelpers.org/questions/28182/how-to-learn-tables User#11440 120 — 7y
Ad

Answer this question