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)
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.
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.
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.
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)
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)
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.
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)