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

How do I make it random?

So I want to make the spawns random, look at lines 17, and 41 through 43. That is mainly where the issue is. Not sure how to do this cause I've tried everything. If someone can help that would be great. :p

local Spawn1 = game.Workspace.Spawn1
local Spawn2 = game.Workspace.Spawn2
local Spawn3 = game.Workspace.Spawn3
local Spawn4 = game.Workspace.Spawn4
local Spawn5 = game.Workspace.Spawn5
local Spawn6 = game.Workspace.Spawn6
local deploy = script.Parent
local gui = deploy.Parent
local g1gui = gui["Gun1GUI"]
local g2gui = gui["Gun2GUI"]
local s1gui = gui["Secondary1GUI"]
local s2gui = gui["Secondary2GUI"]
local pgui = gui.Parent
local player = pgui.Parent
local sselected = ""
local selected = ""
local spawns = {Spawn1, Spawn2, Spawn3, Spawn4, Spawn5, Spawn6}

g1gui.MouseButton1Down:connect(function()
    selected = "Gun1"
end)

g2gui.MouseButton1Down:connect(function()
    selected = "Gun2"
end)

s1gui.MouseButton1Down:connect(function()
	sselected = "Secondary1"
end)

s2gui.MouseButton1Down:connect(function()
	sselected = "Secondary2"
end)

deploy.MouseButton1Down:connect(function()
    if game:GetService("ServerStorage"):FindFirstChild(selected) and player then
        local cloned = game.ServerStorage[selected]:clone()
        cloned.Parent = player.Backpack
		local cloned = game.ServerStorage[sselected]:clone()
        cloned.Parent = player.Backpack
		for i = 1,1 do
		print(spawns[math.random(1,#spawns)])
		player.Character:MoveTo(spawns.Position)
		end
        gui:Destroy()
    end
end)

1 answer

✓ Accepted answer
for i = 1,1 do

The for loop has no use whatsoever, because the starting number is the same as the ending number. It will therefore only repeat once, exactly the same as if it wasn't there at all.


print(spawns[math.random(1,#spawns)])

This will print a random spawn. Okay, cool. Now what?


player.Character:MoveTo(spawns.Position)

This will error. spawns is a list. Lists don't have positions.



You actually correctly got a random spawn on line 42, you just never use it (you only print it). Simply apply what you did.

local chosenSpawn = spawns[math.random(1, #spawns)]
char:MoveTo(chosenSpawn.Position)

1 comment

0

Thanks so much!

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