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

I have a question about math.random?

local Player = game.Players.LocalPlayer
local Cash = Player.leaderstats.Cash

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).

4 comments

0

At least show us the script that wasn't working.

0

If the script is deleted then I can make a fully working one for you.

0

@minecraftjul YES! That would be great. Just post the script in Answers and I can accept the answer if it helps!

0

Added an answer with an edited version of the code supplied, enjoy!

2 answers

✓ Accepted answer

You wanted a script as your answer so here you go:

minValue = 1
maxValue = 10
cash = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Cash")
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
local Cash = Player.leaderstats.Cash

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.

1 comment

0

YES! THANK YOU SO MUCH!

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)
	if (player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Gold")) then
		
		player.leaderstats.Gold.Value = (player.leaderstats.Gold.Value + math.random(100));
	end
end)
	wait(1)
end

So First have a leaderboard with the field of "Gold", then make a part, insert a ClickDetector into said part, Add this script into the ClickDetector.

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