I'm having a leaderstat problem

I’m making a rebirth system for my simulator game! I’m currently having a bug, the bug is that when I’m setting the player’s leaderstat (coins) to 0, it sets to 0 but when I click again on the clicker button, it comes back to how much it was before. Let’s say if it was 100K money before, I rebirth and it sets to 0 then I click again it goes up to 100k again in 1 click. How can I fix this?

Here’s the script:

local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local CoinsNeeded = 100 * Rebirths.Value
local Coins = Player.leaderstats.Coins

if Rebirths == 0 then
	CoinsNeeded = 100 * 1
end

script.Parent["1R"].Text = CoinsNeeded

script.Parent["1R"].MouseButton1Click:Connect(function()
	script.Parent["1R"].Text = CoinsNeeded
	if Player.leaderstats.Coins.Value >= CoinsNeeded then
		Player.leaderstats.Rebirths.Value = Player.leaderstats.Rebirths.Value + 1
		Coins.Value = 0
	else
		print("Not Enough Coins")
	end
end)

All help is appreciated❤️

Hi there,

It appears that the problem is that after the player hits the button, you are not changing the value of CoinsNeeded. CoinsNeeded is currently only set once after script execution and is never modified again.

You can simply reposition the line CoinsNeeded = 100 * Rebirths to correct this.

To be updated each time the player clicks the button, place the value within the MouseButton1Click event.

The revamped/fixed script would appear as follows:

local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local Coins = Player.leaderstats.Coins

script.Parent["1R"].MouseButton1Click:Connect(function()
  local CoinsNeeded = 100 * Rebirths.Value
  if Rebirths.Value == 0 then
    CoinsNeeded = 100
  end

  script.Parent["1R"].Text = CoinsNeeded
  if Player.leaderstats.Coins.Value >= CoinsNeeded then
    Player.leaderstats.Rebirths.Value = Player.leaderstats.Rebirths.Value + 1
    Coins.Value = 0
  else
    print("Not Enough Coins")
  end
end)

By doing this, the value of Rebirths will be used to recalculate CoinsNeeded each time the player presses the button. This ought to resolve the problem with the player’s coins reverting to their original value.

Hope this solves the problem!

Well that did just solve the “not enough coins” system, It didn’t solve anything with the leaderstats. The thing I want to reach is that resets the leaderstats. Now it does not reset datastore, only the stats

I was a little confused because I assumed that was what you wanted, but in any case, perhaps this will help:

local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")

coinsDataStore:SetAsync(Player.UserId, 0)

The player’s coin count will be reset to 0 in the data storage as a result. Other leaderstats in the data store can be reset in a similar manner.

Hope I got it right this time, lol.

Also, I do not know what your data store is called.

local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")

Put this part at the top of the script, like this:

local Player = game.Players.LocalPlayer
local Rebirths = Player.leaderstats.Rebirths
local Coins = Player.leaderstats.Coins
local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")

Then, edit this part

And instead do:

coinsDataStore:SetAsync(Player.UserId, 0)

you can’t have a datastore in the localscript, so that wouldn’t work either. I already have my datastore.

I never noticed the Player part, but anyways to reset the DataStore, you’ll probably have to solve that. I thought it was in a server script, as I never noticed the localPlayer part

Who told you that you can’t do it on the server?

Create a new removeevent and change “Coins.Value = 0” to:

something like: game.ReplicatedStorage.YourNewEvent:FireServer(game.Players.LocalPlayer)

After that, a ServerScript can pick it up and reset the DataStore from that point.

Something like this:

local DataStoreService = game:GetService("DataStoreService")
local coinsDataStore = DataStoreService:GetDataStore("Coins")

game.ReplicatedStorage.YourNewEvent.OnServerEvent:Connect(function(Player)
       coinsDataStore:SetAsync(Player.UserId, 0)
end)

Also you can do it on the Server.
I’ve done that multiple times.

1 Like

nevermind I’ve forgot that, my bad sorry

But don’t do it on the server probably, as we wanna know who clicked it. So try my method once. :smiley:

There is no MouseButton1Down:Connet(function(player)

1 Like

oh yess I should try with remotevents, Thank you.

1 Like

u cannot call localplayer in serverscript so it would be harder to done that tho

I know, but I didn’t see that specific part, sorry.

Anyways try the method I asked you to.

It’s happening because you’re recomputing CoinsNeeded with the old Rebirths.Value after you’ve increased it. You should set CoinsNeeded in the if:
if Rebirths == 0 then
CoinsNeeded = 100 * 1
end

if Rebirths == 0 then
CoinsNeeded = 100 * 1
end

if Rebirths == 0 then
CoinsNeeded = 100 * 1
end

script.Parent[“1R”].Text = CoinsNeeded