Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Spawning at a random spawn?

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

3 comments

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.

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

0

I see. I got you.

1 answer

✓ Accepted answer

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.

3 comments

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 :)

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.

0

ScriptGuider does a great job at explaining tables here, https://scriptinghelpers.org/questions/28182/how-to-learn-tables

This discussion is preserved as it was. New answers, comments, and votes are closed.