Hello Devs, i have so weird problem. I hope i can explain it.
I have a dataStore, Module and Upgrade script. I will give all scripts below text.
Module has upgrade prices as a tables. like first upgrade 100$ second 250$…
When you click imagebutton you earn 1 Strength. And when you make 100 Stregth you go to upgrade place and click upgrade image button and it delete 100 Strength.
Then you click again for save 250 strength. While you clicking upgrade button, for every upgrades the IntValue in Player > sheets folder > Upgrade.Value gets +1
So it changes Module.upgradePrices[UpgradeVal+1]
So everything works perfectly, it saves strenght and your current upgrade value.
The problem is here
- I joined game,
- I have 400 Strenght.
- I upgraded 2 times for 100st and 250st and now i have 400-350 = 50 Strenght and Upgrade Value 2
- I left game and rejoined.
- I still have 2 Upgrade value and 50 Streght
- I started clicking for earn some st. and got 150 strenght and clicked upgrade
- It didn’t upgrade because the next upgrade is 350 Streght
- So i have 150 Streght now.
- I left game and joined back
- I have still 150 Strenght and 2 Upgrade Value
- Now i clicked upgrade again and it upgraded it. It deleted 100 and give you 1 value again.
It shouldn’t upgrade here… It doesn’t make sense.
I hope someone will understand me…
Module in ReplicatedStorage:
local MotherModule = {}
MotherModule.upgradePrices = {
100,
250,
350,
500,
750,
1000
}
return MotherModule
LocalScript in StarterGui
local ScreenGui = Instance.new("ScreenGui")
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local Module = require(game.ReplicatedStorage.Upgrade)
local Upgrade = Module.upgradePrices
local upgradeButton = script.Parent.Upgrade.Frame.Upgrade
ScreenGui.Enabled = true
ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local TextButton = Instance.new("TextButton")
TextButton.Size = UDim2.new(0, 200, 0, 50)
TextButton.Position = UDim2.new(0.5, -100, 0.5, -25)
TextButton.Text = "Strength +1"
TextButton.Parent = ScreenGui
TextButton.MouseButton1Click:Connect(function()
local localplayer = game.Players.LocalPlayer
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
local newValue = player.leaderstats.Strength.Value
RS.UpdateValue:FireServer(newValue, localplayer)
end)
local UpgradeVal = player.sheets.Upgrade.Value
upgradeButton.MouseButton1Click:Connect(function()
local currentUpgrade = Module.upgradePrices[UpgradeVal+1]
local localplayer = game.Players.LocalPlayer
if player.leaderstats.Strength.Value >= currentUpgrade then
player.leaderstats.Strength.Value -= currentUpgrade
UpgradeVal = UpgradeVal + 1
local upvalue = UpgradeVal
RS.UpgradeVal:FireServer(upvalue,localplayer)
local newValue = player.leaderstats.Strength.Value
RS.UpdateValue:FireServer(newValue, localplayer)
else
print("Not enough Strenght")
end
end)
DataStore:
-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore1") -- This can be changed to whatever you want
local RS = game:GetService("ReplicatedStorage")
local updateValue = RS:FindFirstChild("UpdateValue")
local updateVal = RS:FindFirstChild("UpgradeVal")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Strength.Value; -- First value from the table
player.leaderstats.Wins.Value; -- Second value from the table
player.sheets.Upgrade.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
-- // Assigning player stats //
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Sheets = Instance.new("Folder")
Sheets.Name = "sheets"
Sheets.Parent = player
local Strength = Instance.new("IntValue")
Strength.Name = "Strength"
Strength.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local Upgrade = Instance.new("IntValue")
Upgrade.Name = "Upgrade"
Upgrade.Parent = Sheets
local data -- We will define the data here so we can use it later, this data is the table we saved
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
local function values(playerC,newValue)
if player == playerC then
Strength.Value = newValue
end
end
updateValue.OnServerEvent:Connect(values)
local function upgrade(playerC,upvalue)
if player == playerC then
Upgrade.Value = upvalue
end
end
updateVal.OnServerEvent:Connect(upgrade)
end)
if success and data then -- If there were no errors and player loaded the data
Strength.Value = data[1] -- Set the money to the first value of the table (data)
Wins.Value = data[2]
Upgrade.Value = data[3]-- Set the coins to the second value of the table (data)
else -- The player didn't load in the data, and probably is a new player
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)