function onTouched() local partchoice = math.random(1,3) if partchoice == 1 then game.Workspace.Part1:Destroy() script.Parent:Destroy() end if partchoice == 2 then game.Workspace.Part2:Destroy() script.Parent:Destroy() end if partchoice == 3 then game.Workspace.Part2:Destroy() script.Parent:Destroy() end end script.Parent.Touched:connect (onTouched)
I put this script inside a part, that when touched, picks a random choice to do... but my problem is that is always does the first one. Part 1 is the only one that gets removed every time, and i dont know why :( help anyone? :/
There is no issue, the only problem is the human mind. Why? Because humans have the power to see patterns in everything1. Something that machines don't have. So even though something is as random as possible it may not seem random at all because we spot a pattern. That's why making something random is so difficult. So roblox has something called a randomseed
which is a seed that the pseudo-random generator refers to. Kind of like how minecraft you need a seed to load a certain maps. Equal seeds create equal outcomes.
Math.random has it's own default seed. Whenever you run it you'll always get the same sequence of numbers.
print(math.random(100))
89
When you restart the game it'll still print
89
How do you get this to change? You change the randomseed.
math.randomseed(2) --changed the seed, it must be a number value. print(math.random(100))
1
But we return to the original problem, it doesn't stop printing 1 the first time!
We need to change the random seed to a never changing number, what's a number that never stops... Time! Time never stops. But how do we get time? tick()
or os.time
. We'll use tick()
because it's easier. tick()
is the local or UNIX time. Basically the time on your computer right now. tick()
returns the number of seconds since the epoch or 1 January 1970, 00:00:00. So we can make the random seed that value.
Please note that math.randomseed does NOT have to be placed at the beginning of the script.
function onTouched() math.randomseed(tick()) local partchoice = math.random(1,3) if partchoice == 1 then game.Workspace.Part1:Destroy() script.Parent:Destroy() end if partchoice == 2 then game.Workspace.Part2:Destroy() script.Parent:Destroy() end if partchoice == 3 then game.Workspace.Part2:Destroy() script.Parent:Destroy() end end script.Parent.Touched:connect (onTouched)
Hope it helps!
What is Random? by VSause ↩
I have this issue alot, first at the beginning of your script, you should do: math.randomseed(tick())
also, make a variable called like lastpartchoice, and set that to part choice. Then check if partchoice is equal to lastpartchoice, call math.random again. Also, you can replace line 6 - 14 with:
else game.Workspace.Part2:Destroy() script.Parent:Destroy() end
and you can make math.random to math.random(2) however you may have ment to destroy part3 when partchoice = 3.
First, IT IS RANDOM WHAT DO YOU WANT FROM IT. Second, can't we have better script like this?? You can use some elseif's, it is not going to blow up your game.
function onTouched() local partchoice = math.random(1,3) if partchoice == 1 then game.Workspace.Part1:Destroy() script.Parent:Destroy() elseif partchoice == 2 then game.Workspace.Part2:Destroy() script.Parent:Destroy() elseif partchoice == 3 then game.Workspace.Part2:Destroy() script.Parent:Destroy() end end script.Parent.Touched:connect (onTouched)