Math.random() does not choose more than 2 results?

Basically, math.random() does not choose more than two results. Here’s the script:
script.Parent.ClickDetector.MouseClick:Connect(function(henlo)
script.Parent.Sound:Play()
for i = 0, 1, 0.09 do
wait(i/50)
script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.Parent.Button1.CFrame, i)
end
local e = math.random(1, 10)
if e == 1 then
print(“Method: GET CRUSHED BY A WEIGHTED COMPANION CUBE”)
workspace.CompCube.Anchored = false
end
if e == 2 then
print(“Method: GET CRUSHED BY A HEAVY DUTY CRUSHER”)
workspace.crosher:Play()
for z = 0, 1, 0.1 do
wait(z/50)
workspace.Crusher.CFrame = workspace.Crusher.CFrame:Lerp(workspace.Crusher.Crusher1.CFrame, z)
end
if e == 3 then
print(e)
end
if e == 4 then
print(e)
end
if e == 5 then
print(e)
end
if e == 6 then
print(e)
end
if e == 7 then
print(e)
end
if e == 8 then
print(e)
end
if e == 9 then
print(e)
end
if e == 10 then
print(e)
end
end
end)
BTW, there’s no error in the output. I saw this happening to another guy, nobody could fix it on scripting helpers… Hope I gain results here.

1 Like

Please format your code, as written here.

https://devforum.roblox.com/t/developer-forum-formatting-guide/456083

It will be easier to read. :wink:

1 Like

Hello there, :smile:

Before I try and fix this problem, I highly recommend you to use the lua format for your code for easier readability:

luaformat

theformat

Alright, I will need to know a few things before I try and fix the problem so I can know what’s going on:

  • 1 - Can you please be more specific of what you are trying to achieve? e.g: What is the purpose of this script/local script?
  • 2 - Is this a script or a local script? and where did you put it?
  • 3 - Did you try printing “e”? if not can you print “e” and show me the result(s)?
  • 4 - Can you put a print at the start of the MouseClick function so we can know if it actually works or not?
  • 5 - Is it possible for you to use Random.new():NextInteger() instead of math.random()? if not then what is the reason?
  • 6 - Can you send us an image which shows the ClickDetector’s parent location and it’s parent?

u should use

elseif

not restarting it

math.random returns a (/one) pseudo-random whole number between 2 specified numbers, call it how many times you want a random result

The purpose of this script is to pick between 10 different methods on how to kill someone… This is a script, I parented it to the button which you press to activate the random method, I tried printing E, there were no results in the output, no errors or nothing, yes the mouseclick works, method 1 and 2 works, I’ll try that now, and image

1 Like

remodify your E’s the math random value

see this code

local e = math.random(0,10)
if e == 0 then
	print(0)
elseif e == 1 then
	print(1)
elseif e == 2 then
	print(2)
elseif e == 3 then
	print(3)
elseif e == 4 then
	print(4)
elseif e == 5 then
	print(5)
elseif e == 6 then
	print(6)
elseif e == 7 then
	print(7)
elseif e == 8 then
	print(8)
elseif e == 9 then
	print(9)
elseif e == 10 then
	print(10)
end

also, one and two works as shown here:

This is 1:
image
This is 2:
image

Get this code and try it

script.Parent.ClickDetector.MouseClick:Connect(function(henlo)
script.Parent.Sound:Play()
for i = 0, 1, 0.09 do
wait(i/50)
script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.Parent.Button1.CFrame, i)
end
local e = math.random(1, 10)
if e == 1 then
print("Method: GET CRUSHED BY A WEIGHTED COMPANION CUBE")
workspace.CompCube.Anchored = false

elseif e == 2 then
print("Method: GET CRUSHED BY A HEAVY DUTY CRUSHER")
workspace.crosher:Play()
for z = 0, 1, 0.1 do
wait(z/50)
workspace.Crusher.CFrame = workspace.Crusher.CFrame:Lerp(workspace.Crusher.Crusher1.CFrame, z)
end
elseif e == 3 then
print(e)

elseif e == 4 then
print(e)

elseif e == 5 then
print(e)

elseif e == 6 then
print(e)

elseif e == 7 then
print(e)

elseif e == 8 then
print(e)

elseif e == 9 then
print(e)

elseif e == 10 then
print(e)

end
end)

Oh my god it works thanks!!!

Alright, I identified the problem, basically for some reason the “e” variable which contains the random number values won’t print which is why when it selects a number other than 1 and 2 it won’t do anything because your telling the script to print the variable “e” if it selects the other numbers.

Your script works fine, however I’m not sure why e isn’t printing in the output, perhaps you can’t print random number values but that shouldn’t concern you because your script works, there’s no problem in it other than that it’s not printing the variable “e” althought that’s a very minor problem.

1 Like

Does it work or not? 30charssss

1 Like

Yep! It works!!!

Nerkonl, sorry for wasting your time… The script need elseif instead of if… I’m glad you helped me with this though :smiley:

You can shorten your code almost in half by doing this:

script.Parent.ClickDetector.MouseClick:Connect(function(henlo)
script.Parent.Sound:Play()
for i = 0, 1, 0.09 do
wait(i/50)
script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.Parent.Button1.CFrame, i)
end
local e = math.random(1, 10)
if e == 1 then
print("Method: GET CRUSHED BY A WEIGHTED COMPANION CUBE")
workspace.CompCube.Anchored = false

elseif e == 2 then
print("Method: GET CRUSHED BY A HEAVY DUTY CRUSHER")
workspace.crosher:Play()
for z = 0, 1, 0.1 do
wait(z/50)
workspace.Crusher.CFrame = workspace.Crusher.CFrame:Lerp(workspace.Crusher.Crusher1.CFrame, z)
end
elseif e >= 3 and <= 10 then
    Print(e)
end

There’s no point in making a new if statement for every possible value if the command is the same for each of them.

This was happening because an “end” from one of the “if” expressions appeared to be all the way at the bottom of the script.

image

Here is a fixed version (hopefully)

script.Parent.ClickDetector.MouseClick:Connect(function(henlo)
script.Parent.Sound:Play()
for i = 0, 1, 0.09 do
wait(i/50)
script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.Parent.Button1.CFrame, i)
end
local e = math.random(1, 10)
if e == 1 then
print(“Method: GET CRUSHED BY A WEIGHTED COMPANION CUBE”)
workspace.CompCube.Anchored = false
end
if e == 2 then
print(“Method: GET CRUSHED BY A HEAVY DUTY CRUSHER”)
workspace.crosher:Play()
for z = 0, 1, 0.1 do
wait(z/50)
workspace.Crusher.CFrame = workspace.Crusher.CFrame:Lerp(workspace.Crusher.Crusher1.CFrame, z)
end
end
if e == 3 then
print(e)
end
if e == 4 then
print(e)
end
if e == 5 then
print(e)
end
if e == 6 then
print(e)
end
if e == 7 then
print(e)
end
if e == 8 then
print(e)
end
if e == 9 then
print(e)
end
if e == 10 then
print(e)
end
end)

I know this is solved, just pointing out the problem. :slight_smile:

1 Like

I assume OP used a common command simply to debug, but under each conditional statement he’d eventually add a new, different function rather than grouping a single function to increase its chance of being called.


@Auxigin, FerbZides’s suggestion has proven to work properly and I agree it’s the best fix for this code! (Note that Floo_d pointed the real root of the problem, a missplaced end keyword)

However, I highly recommend you to dig deep into the amazing world of tables and arrays for future works. They can open you a lot of possibilities! In this case, for example, using them would make it much easier to debug and, especially, to update the code (it could be very annoying if you now wanted 11 events instead of 10).