Won't add +1 value to leaderstats

I’m trying to make it so like every time the countdown reaches 0 the server adds a +1 value to the players leader stats.(I want this to function in fe). Here is the code. No errors or nothing it just doesn’t add a value to the leader stats. Countdown still works though.

local seconds = script.Parent.Parent.Time

script.Parent.Text = "Pay in: "…seconds.Value

while true do wait()
for i = 1,seconds.Value do
wait(1)
seconds.Value = seconds.Value -1
script.Parent.Text = "Pay in: "…seconds.Value
if seconds.Value == 0 then
seconds.Value = 120
if seconds.Value == 0 then
game:GetService(“Players”):FindFirstChild(game.Players.LocalPlayer.Name):FindFirstChild(“leaderstats”):FindFirstChild(“Wallet”).Value = game:GetService(“Players”):FindFirstChild(game.Players.LocalPlayer.Name):FindFirstChild(“leaderstats”):FindFirstChild(“Wallet”).Value + 1
end
end
end
end

1 Like

Try doing this:

local seconds = script.Parent.Parent.Time.Value

script.Parent.Text = "Loading..."

while true do 
wait(1)


seconds = seconds - 1

script.Parent.Text = "Pay in: "..seconds.." Seconds"

if seconds.Value == 0 then
seconds.Value = 120

game.Players.LocalPlayer.leaderstats.Wallet.Value = game.Players.LocalPlayer.leaderstats.Wallet.Value + 1

end
end




I got rid of some code that was unneeded and cleaned it up a bit.

Just realized something IMPORTANT: This script ONLY works if its a local script.

This should stand out better after formatting your code correctly. seconds.Value isn’t 0 anymore after you change it to 120.

It would also save a bit of space to store the path to Wallet in a variable instead of writing the whole thing out twice.

(there are additional problems on the way to your end goal but this is the one your post is about. since you’re referencing LocalPlayer, this must be on the client, so changes to Wallet will not replicate.)

2 Likes

if filtering enabled is on then you will need to have a remote event so you can communicate from client to server by doing this:

lets say the remote event is in replicated storage

Client:

local seconds = script.Parent.Parent.Time.Value

script.Parent.Text = "Loading..."

while true do 
wait(1)


seconds.Value = seconds.Value  - 1

script.Parent.Text = "Pay in: "..seconds.Value.." Seconds"

if seconds.Value == 0 then
seconds.Value = 120

game:GetService("ReplicatedStorage")["RemoteEvent"]:FireServer(1)
end
end

Server:

game.ReplicatedStorage["RemoteEvent"].OnServerEvent:Connect(function(player, amount)
player.leaderstats.Wallet.Value = player.leaderstats.Wallet.Value + amount
end

Hope this helped.

1 Like

Such a remote would be extremely vulnerable to exploits. Someone could easily call that remote from the client and give themselves as much money as they want, whenever they want.

2 Likes

Then perhaps this is a teaching lesson for us, can you please tell us how we can avoid exploiters from firing the event?

Exploiters tend to run something called “Remote Spy” or “Remote Sniffer,” which will print messages to their Dev Console (F9). They use this to figure out the name of the remote’s that you have in your game, and even what arguments are being used so that they can try and abuse your replication logic.

In this case I would recommend just handling the countdown on the server and then also rewarding the player from there. If it were a large game and I want to cut down on server lag, then I’d want to avoid unnecessary loops on the server (especially if it’s for every player) and would probably go with my edited comment below.

Edit: If it’s always expected to be just +1 to the existing amount, you could also track the last time the remote was called on the server (using os.time()) and compare. You’d have to remove the amount parameter though, because then the exploiter could spam call with a high value until the next pay time.

2 Likes

You can’t prevent exploiters from firing an event silly!

You should NEVER trust the client to handle ANYTHING important. The client should literally only be there for displaying UIs and sending UserInput. (ok that’s not entirely true, there are some exceptions, but do follow it as closely as possible!)

Anyways, put all of the essential values like seconds on the server.

--Server
while true do 
wait(1)
seconds.Value = seconds.Value  - 1
game:GetService("ReplicatedStorage")["RemoteEvent"]:FireClient(plr, seconds.Value) --Ok you really shouldn't do this and this is super wasteful, but this is just a demonstration on safety, not practicality
if seconds.Value == 0 then
seconds.Value = 120

player.leaderstats.Wallet.Value = player.leaderstats.Wallet.Value + (amount or 1) --Value defaults to 1. Set an "amount" variable to change amount
end
--Client
game.ReplicatedStorage["RemoteEvent"].OnClientEvent:Connect(function(player, timeValue)
      script.Parent.Text = "Pay in: "..timeValue.." Seconds"
end

You should never in a million years do anything such as:

Any 7 year old can get a free Roblox “hack” (they should really be called exploits, but searching up “Roblox hack” works just fine) straight from a questionable legal/illegal website and send

game:GetService("ReplicatedStorage")["RemoteEvent"]:FireServer(99999999999)

Again - the client should only be there for UIs and UserInput. Pretend as if every single player is the worlds best and most intuitive hacker, but you’re pretending like they’re a normal player.

2 Likes

Thank youuu. This helped a lot.

Hold on though. I did everything that you said and I got this error. image

The code block which he has commented with “Server” should be put into a ServerScript, which you can place in eg ServerScriptService

Ok I did that. No errors now but it still wont + the value

GUI:
image

Server:
image


(I just changed RemoteEvent to Remote but it still won’t work)

If you change the name of the Remote you also need to change the name in you’re scripts here
image
and here

Yea thats what I was saying. I did that

What Error is showing inside the Output?

https://gyazo.com/2c4087cd6fff377d04cc8641f643137e

Its showing nothing, nothing in the server either

Create print(“”) and see where the code is running and which part its not

Still nothing. The print doesnt even show up