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

How to add random amount of cash every 10 seconds?

Asked by 1 year ago
Edited 1 year ago

hi, i'm trying to make it so that the game gives a random amount of cash ($0.00 to $10.00) to each player (each player gets a different amount) for every 10 seconds.

My goal is to kind of recreate the game "Street Simulator" roblox.com/games/3823850583 just as a little solo project and not intending to release this game to the public for obvious reasons.

I put a script in ServerScriptService:

local Player = game:GetService("Players").LocalPlayer
local randomcash = math.random(0.01, 10.00)
local Cash = Player.leaderstats:WaitForChild("Cash")
while true do
    Cash.Value = Cash.Value + randomcash
        wait(10)
end

problem: the code does not work.

2 answers

Log in to vote
1
Answered by 1 year ago

Values such as money should be changed on the server. This is because if it's changed on the client, it will not be saved / viewed by the server. Meaning only the client can see what it's been changed to. This means you can **not ** use local scripts.

Next point is that scripts in ServerStorage do not work. But I believe you were meant to put serverscriptservice.

Finally, game:GetService("Players").LocalPlayer does not work on the server. This may be the problem if those over two points are allready covered. This is because the client so local scripts have a local player as they only run on that one player, when server scripts dont as they run on the server.

I am guessing you are wanting this to run for every player, there is a way to do this.

game.Players.PlayerAdded:Connect(function(Player)
    local randomcash = math.random(0.01, 10.00)
    local Cash = Player.leaderstats:WaitForChild("Cash")
    while true do
        Cash.Value = Cash.Value + randomcash
             wait(10)
    end
end)

And that should be your new server script, in serverscriptservice.

I am going to now explain the two lines of code I added: game.Players.PlayerAdded:Connect(function(Player) will simply run the code up untill end) when any player joins the game. It will return Player as the player that joined replacing the game:GetService("Players").LocalPlayer line, making the code run on the server.

Ad
Log in to vote
0
Answered by 1 year ago

The other person was on the right path and noticed something pretty big! But another part is where the while true do loop is. Here's my script if you want any explainations I'd gladly give!

game.Players.PlayerAdded:Connect(function(Player)
local Cash = Player.leaderstats:WaitForChild("Cash")
while true do
wait(10)
    local randomcash = math.random(0.01, 10.00)
    Cash.Value = Cash.Value + randomcash
    print(randomcash)
    end
end)

0
You should get rid of the print, I just put it there so I could make sure the random cash was adding up when I didn't connect it to a leaderboard darkkitten12345 8 — 1y

Answer this question