Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

I created a text button and put the script to when press it gives random coins to 1 to 100 coins

01math.randomseed(tick())
02local random = math.random(1,100)
03function OnClick(Plr)
04    script.Disabled = true
05    if Random >= 1 or Random <= 50 then
06local Stats = Plr:WaitForChild("Stats").Coins
07Stats.Value = Stats.Value math.random(1,100)
08    elseif Random  >= 51 or Random <= 100 then
09        local Stats = Plr:WaitForChild("Stats").Coins
10        Stats.Value = Stats.Value math.random(1,100)
11    end
12end

3 answers

Log in to vote
0
Answered by 5 years ago

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.

1-- Button Script
2local button = script.Parent
3local remote = game.ReplicatedStorage:WaitForChild('CoinRemote') -- Wait for the remote to be makde
4 
5button.MouseButton1Down:Connect(function()
6    remote:FireServer() -- Tell the server to give us random amounts of coins
7end)
01-- Server Script
02 
03-- Create a new remote event for the button
04local remote = Instance.new('RemoteEvent')
05remote.Name = 'CoinRemote'
06remote.Parent = game.ReplicatedStorage
07 
08-- Create an event for the remote event
09remote.OnServerEvent:Connect(function(player) -- client telling us to activate
10    local leaderstats = player:FindFirstChild('leaderstats') -- check if the player has the leaderstats
11    if leaderstats then
12        local coins = leaderstats['Coins'] -- get their coins
13        coins.Value = coins.Value + math.random(1, 100) -- add randonmly 1-100 coins
14    end
15end)
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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 :

1math.randomseed(tick())
2function OnClick(Plr)
3local random = math.random(1,100) -- here, so it creates a new random number each time you press the button
4local Stats = Plr:WaitForChild("Stats").Coins
5Stats.Value = Stats.Value + random
6script.Disabled = true -- remove this line if you want players to be able to use it many times
7end

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

0
i wanted whenever a player clicks text button it gives them random coins from 1 to 100 mastercheeseit 77 — 5y
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) fighterkirbyzx 102 — 5y
0
I've changed the script a bit so tell me if it's still not what you wanted NotZuraax 68 — 5y
0
didn't work i've tried mastercheeseit 77 — 5y
View all comments (4 more)
0
You're sure you modified the script so it works with your button ? NotZuraax 68 — 5y
0
yeah mastercheeseit 77 — 5y
0
Well then the error come from your script, caus i tested mine from line 3 and it works NotZuraax 68 — 5y
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 mastercheeseit 77 — 5y
Log in to vote
0
Answered by
Arj783 72
5 years ago
Edited 5 years ago
1script.Parent.MouseButton1Click:Connect(function(Plr)
2local r = math.random(1, 100)
3plr:WaitForChild("Stats"):WaitForChild("Coins").Value = plr:WaitForChild("Stats"):WaitForChild("Coins").Value + r
4end)

That should be it...

Made some edits

0
didn't work mastercheeseit 77 — 5y

Answer this question