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

[ANSWERED] How to do random coins between 1 and 100 why doesn't it work?

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

3 answers

✓ Accepted answer

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

8 comments

0

i wanted whenever a player clicks text button it gives them random coins from 1 to 100

0

Also im not sure why you disabled the script, if you do need to disable the script you should put script.Disabled at the end (inside the function)

0

I've changed the script a bit so tell me if it's still not what you wanted

0

didn't work i've tried

0

You're sure you modified the script so it works with your button ?

0

yeah

0

Well then the error come from your script, caus i tested mine from line 3 and it works

0

maybe its because of the function the output says Players.mastercheeseit.PlayerGui.Gui.Settings.List.Frame.TextButton.LocalScript:2: Incomplete statement: expected assignment or a function call

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

1 comment

0

didn't work

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