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

Hello Forum!! Randomized CanCollide?

Asked by 10 years ago

Hello i was wondering how i would come about this what i am trying to do is create a script that makes 2 out of the 3 blocks walk throughable but im having troubles where the randomize sometimes adds up to be the same number making all three or just one un-collidable. how would i go about this?

1 answer

Log in to vote
1
Answered by 10 years ago

Assuming we have Part1,Part2 and Part3:

Bricks = {game.Workspace.Part1,game.Workspace.Part2,game.Workspace.Part3} -- We save our parts in a table
RandomBricks = {} -- We create a table that will hold our randomised bricks
Amount = 2 -- We chose the amount of randombricks

for i = 1,Amount do -- We execute the following code the amount of time as we want to have bricks
    local random = math.random(1,#Bricks) -- We chose a random entry in the table
    table.insert(RandomBricks,{Bricks[random]}) -- We save our randomly chosen brick
    table.remove(Bricks,random) -- We remove our randomly chosen brick from the other bricks, so it cannot be picked
end

for _,v in pairs (RandomBricks) do -- For each randomly chosen brick we execute the following code with v being the brick
    v.CanCollide = false -- We set cancollide to false
    table.insert(Bricks, v) -- We put the brick back with all the other bricks
end

RandomBricks = {} -- We empty our table with random bricks so we can safely rerun the code

Above is the most flexible way (I can think of) to do this as you only have to edit the variables Bricks and Amount. You can also replace the table in Variable to game.Workspace.ModelName:GetChildren() if you wanted to use all the children of a model.

If you are simply looking for a way to get 2 randomised numbers that are not the same, this is an easy way to do that:

random1 = math.random(1,3)
repeat random2 = math.random(1,3) until random2 ~= random1
0
Ingenious! Thank you i never would have thought of using repeat! Chaserox 62 — 10y
1
No problem! Perhaps you could accept my answer as the correct one? juzzbyXfalcon 155 — 10y
Ad

Answer this question