I need help on my script

Hi guys, I stumbled upon a problem in my game, and I don’t know how to fix it.
The Problem:
When someone buys a tool with credits in my game, everything works except for the fact that when they leave they get the credits back ; -;
Expected action
I would expect for the credits to stay gone, even when they rejoin, but of course that doesn’t happen.
The script:

player = game.Players.LocalPlayer
backpack = player.Backpack

script.Parent.MouseButton1Click:Connect(function()
	local SpeedCoil = game.ReplicatedStorage.Tools.SpeedCoil:Clone()
	if player.leaderstats.Credits.Value >= 100 then
		player.leaderstats.Credits.Value = player.leaderstats.Credits.Value - 100 
	end
end)

So yeah I want the credits to stay gone.

9 Likes

You are subtracting credits in a LocalScript, but saving them on a ServerScript. It will not replicate. Use a RemoteEvent to change the credits so they replicate to the server.

3 Likes

The LocalScript is on a textbutton, sorry if im misunderstanding. Im very new when it comes to scripting.

1 Like

I’d recommend putting a RemoteEvent in ReplicatedStorage, and firing the RemoteEvent when the player makes the purchase. You will need to tell the server how much it needs to remove, and from which player.

2 Likes

Well do you have an data store setup for the currency system? If not then you definitely need a data store and it’d save the players leaderstats and when they rejoin the game their data would automatically be loaded.

3 Likes

yes, i already have a datastore for the currency system, do i need to add a datastore to the local script? so like it saves when they buy it?

3 Likes

Can you write that in a script, when it comes to scripting im an idiot.

2 Likes

There must be some type of error with your data store script then. Can you send the script here?

Also when sending it make sure to put three ``` at the top of your message and three `'s at the bottom of your message.

It should look like this,

`` `
Example
`` `

Remove the spaces between the `'s though.

3 Likes

Here is the datastore script (the datastore and the leaderstats r separate)

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Wins
	local save2 = plr.leaderstats.Credits
	local GetSaved = ds:GetAsync(plrid)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value}
		local NumberForSaving = {save2.Value}
		ds:GetAsync(plrid, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Wins.Value,plr.leaderstats.Credits.Value})
end)
1 Like

Yeah the script doesn’t function properly, I can give you an DataStore script that you can use.

3 Likes

Ok.
(ignore: minimum characters)

I’m so sorry for not responding I can send the data store script now.

Here is the script,

local DataStore = game:GetService("DataStoreService")
local CurrencyStorage = DataStore:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(Player)

    local CurrencyValue
    local success, err = pcall(function()
       CurrencyValue =  CurrencyStorage:GetAsync("Player_"..Player.UserId)
    end)
    
	local Statistics = Instance.new("Folder")
	Statistics.Name = "Statistics"
    Statistics.Parent = Player


	local Currency = Instance.new("IntValue")
	Currency.Name = "Currency"
	Currency.Parent = Statistics

    if success then
        Currency.Value = CurrencyValue
    else
       print("Failed to load saved data.")
    end
end)

local function Save(Player)
	local success, err = pcall(function()
		CurrencyStorage:SetAsync("Player_"..Player.UserId, Player.Statistics.Currency.Value)
	end)
    if success then
        print("The player's data has been successfully saved.")
    else
        print("Failed to save the player's data.")
    end
end

local function AutomaticSave()
    while wait(1) do
        for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
            Save(Player)
        end
    end
end

spawn(AutomaticSave)

And here is also the file for the script,

Currency Data Store Script.lua (1.2 KB)

Also for the statistics part you will need to rename all of that to leaderstats and rename currency to what you want, also remove the leaderstats creation script you already have as that script will manage the currency data store system.

2 Likes

I will try this tomorrow since it almost 2 AM for me, btw how would I add another stat (wins and credits)

Just make two number values create inside of the leaderstats folder and name one of them Wins and the other Credits.

3 Likes

So like this.

local DataStore = game:GetService("DataStoreService")
local WinsStorage = DataStore:GetDataStore("Wins")
local CreditsStorage = DataStore:GetDataStore("Credits")

game.Players.PlayerAdded:Connect(function(Player)

    local WinsValue
    local CreditsValue
    local success, err = pcall(function()
       WinsValue = WinsStorage:GetAsync("Player_"..Player.UserId)
       CreditsValue =  CreditsStorage:GetAsync("Player_"..Player.UserId)
    end)
    
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player


	local Wins = Instance.new("NumberValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats

	local Credits = Instance.new("NumberValue")
	Credits.Name = "Credits"
	Credits.Parent = leaderstats


    if success then
        Wins.Value = WinsValue
        Credits.Value = CreditsValue
    else
       print("Failed to load saved data.")
    end
end)

local function Save(Player)
	local success, err = pcall(function()
		WinsStorage:SetAsync("Player_"..Player.UserId, Player.leaderstats.Wins.Value)
		CreditsStorage:SetAsync("Player_"..Player.UserId, Player.leaderstats.Credits.Value)
	end)
    if success then
        print("The player's data has been successfully saved.")
    else
        print("Failed to save the player's data.")
    end
end

local function AutomaticSave()
    while wait(1) do
        for i, Player in pairs(game:GetService("Players"):GetPlayers()) do
            Save(Player)
        end
    end
end

spawn(AutomaticSave)
2 Likes

Ok, I’ll try that when I wake up tomorrow. I hope this works :+1:

2 Likes

It should most definitely work but like I said make sure to delete the script that creates the leaderstats folder.

1 Like

kk, would that reset every players stats?