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 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 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.

-- 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)
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 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 :

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

0
i wanted whenever a player clicks text button it gives them random coins from 1 to 100 mastercheeseit 77 — 4y
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 — 4y
0
I've changed the script a bit so tell me if it's still not what you wanted NotZuraax 68 — 4y
0
didn't work i've tried mastercheeseit 77 — 4y
View all comments (4 more)
0
You're sure you modified the script so it works with your button ? NotZuraax 68 — 4y
0
yeah mastercheeseit 77 — 4y
0
Well then the error come from your script, caus i tested mine from line 3 and it works NotZuraax 68 — 4y
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 — 4y
Log in to vote
0
Answered by
Arj783 72
4 years ago
Edited 4 years ago
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

0
didn't work mastercheeseit 77 — 4y

Answer this question