0

# I have a question about math.random?

Edited 7 years ago
local Player = game.Players.LocalPlayer

function onClick()
Cashie = math.random(1,3)
end
script.Parent.MouseButton1Click:connect(onClick)

if Cashie == 1 then
Cash.Value = Cash.Value + 100
end
if Cashie == 2 then
Cash.Value = Cash.Value + 200
end
if Cashie == 3 then
Cash.Value = Cash.Value + 300
end


So, I'm trying to get a script to pick a random amount of cash upon the click of a button (using math.random) So, How would I get the math.random to work when I click a button? Yes, I have attempted this by myself already, but the script wasn't working (there was nothing in Output either).

0
At least show us the script that wasn't working. Programical 653 — 7y
0
If the script is deleted then I can make a fully working one for you. MineJulRBX 52 — 7y
0
@minecraftjul YES! That would be great. Just post the script in Answers and I can accept the answer if it helps! IfIWasntSoSwag 98 — 7y
0
Added an answer with an edited version of the code supplied, enjoy! General_Scripter 425 — 7y

0
Edited 7 years ago

minValue = 1
maxValue = 10
script.Parent.MouseButton1Click:connect(function()
cash.Value = cash.Value+math.random(minValue,maxValue)
end)


This script was made assuming it's parent was the button, the game is FilteringEnabled false and the cash stat is a leaderstat called Cash. I'm sure that as you've had a go yourself you'll understand the ways you can edit this.

Ok, I saw the code you supplied, your problem was just that the logic was out of the function so being run when the script is run but no on the button click, here's the fixed version:

local Player = game.Players.LocalPlayer

function onClick()
Cashie = math.random(1,3)
if Cashie == 1 then
Cash.Value = Cash.Value + 100
elseif Cashie == 2 then
Cash.Value = Cash.Value + 200
elseif Cashie == 3 then
Cash.Value = Cash.Value + 300
end
end
script.Parent.MouseButton1Click:connect(onClick)


Enjoy and accept it as the answer if it works as you wanted. If it didn't let me know in the comments below with as much detail as possible.

0
YES! THANK YOU SO MUCH! IfIWasntSoSwag 98 — 7y
0
Edited 7 years ago

Here is a script I modified from one of my games, when you click the brick it activates a clickdetector which activates a script inside the clickdetector.

math.randomseed(tick())
for _ = 1, 10 do
script.Parent.MouseClick:connect(function(player)