I created a text button and put the script to when press it gives random coins to 1 to 100 coins
math.randomseed(tick()) local random = math.random(1,100) function OnClick(Plr) script.Disabled = true if Random >= 1 or Random <= 50 then local Stats = Plr:WaitForChild("Stats").Coins Stats.Value = Stats.Value math.random(1,100) elseif Random >= 51 or Random <= 100 then local Stats = Plr:WaitForChild("Stats").Coins Stats.Value = Stats.Value math.random(1,100) end end
Well to start, we must remember that we're doing this on the SERVER only, as we want to edit the player's coins. I'll use remote events to tell the server when to give players coins.
-- Button Script local button = script.Parent local remote = game.ReplicatedStorage:WaitForChild('CoinRemote') -- Wait for the remote to be makde button.MouseButton1Down:Connect(function() remote:FireServer() -- Tell the server to give us random amounts of coins end)
-- Server Script -- Create a new remote event for the button local remote = Instance.new('RemoteEvent') remote.Name = 'CoinRemote' remote.Parent = game.ReplicatedStorage -- Create an event for the remote event remote.OnServerEvent:Connect(function(player) -- client telling us to activate local leaderstats = player:FindFirstChild('leaderstats') -- check if the player has the leaderstats if leaderstats then local coins = leaderstats['Coins'] -- get their coins coins.Value = coins.Value + math.random(1, 100) -- add randonmly 1-100 coins end end)
You must put the random variable inside the function. If you keep it outside, it will give a random number at the beginning but it won't change.
EDITED : If i've correctly understood what you wanted, maybe this will work like this :
math.randomseed(tick()) function OnClick(Plr) local random = math.random(1,100) -- here, so it creates a new random number each time you press the button local Stats = Plr:WaitForChild("Stats").Coins Stats.Value = Stats.Value + random script.Disabled = true -- remove this line if you want players to be able to use it many times end
So, tell me if there's still a mistake, if this is not what you wanted : this actual script will, when pressing the button, create a random number between 1 and 100. It will then add the random number stored in the 'random' variable to the value of Coins, located in Plr.Stats. Finally, it will disable the script so players can only press it once
All you need to do now is to link the script to the button you want to activate the script
script.Parent.MouseButton1Click:Connect(function(Plr) local r = math.random(1, 100) plr:WaitForChild("Stats"):WaitForChild("Coins").Value = plr:WaitForChild("Stats"):WaitForChild("Coins").Value + r end)
That should be it...
Made some edits