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