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

Level generator when a button is pressed?

I want to make a game in which, by clicking on the button from "Server Storage", the game will randomly select one of 3 levels. The first will be "level1", the second "level2", the third "level3". In this case, x of the level chosen by the random should be shifted by "20" (relative to the button). I don’t know how to write it, but I have a simple script that from "Server Storage" clones "level1" and transfers it to the middle of the map.

function onClicked ()
	local level = game.ServerStorage.Level1:Clone()
	level.Parent = game.Workspace
	level.Position = Vector3.new(0, 0.5, 0)
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

The only thing that causes me difficulties is a random choice from three levels of one, and moving this level "20 x" relative to the button.

Each level is made of bricks and is in the model. All three levels are anchor. Can collide is on.

1 answer

✓ Accepted answer

Here is an example, if those levels are models then you can not set their position, instead in properties of the model you set it's primary part and set it's cframe. Example:

local ss = game:GetService("ServerStorage") -- server storage

local levels = ss:GetChildren() -- better put them into a folder

local random_level = levels[math.random(1, #levels)] -- it will choose one by generating number from 1 to amount of children

random_level.Parent = workspace

-- random_level.Position = Vector3.new(0,0,0) -- NO

random_level:SetPrimaryPartCFrame(CFrame.new(0, 16, 0))

It will choose random level and spawn it 16 studs high.

I reccomend keeping those levels in a specific folder because if you add any other object into server storage then it will choose it too. Hope i understood you right and this is what you wanted.

1 comment

0

Yes, you understood correctly. Thanks for the help.

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