So I am making a game called Sweet Empire, and I am currently working on my leaderstats/saving script. I just want to know, how can I save a variable? Like: local variable = 250
This is my script:
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore1 = dataStoreService:GetDataStore("SaveCashDataII")
local xpNeeded = 350
local function Save(plr)
local key = "plr-"..plr.UserId
local save = {
["Cash"] = plr.leaderstats.Cash.Value,
["Level"] = plr.leaderstats.Level.Value,
["XP"] = plr.leaderstats.XP.Value,
["RequireXP"] = xpNeeded
}
local success, err = pcall(function()
dataStore1:SetAsync(key, save)
end)
if not success then
warn("Failed to overwrite data: "..tostring(err))
end
end
local function Load(plr)
local xpNeeded2
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 500
cash.Parent = leaderstats
local lvl = Instance.new("NumberValue")
lvl.Name = "Level"
lvl.Value = 1
lvl.Parent = leaderstats
local xp = Instance.new("NumberValue")
xp.Name = "XP"
xp.Value = 0
xp.Parent = leaderstats
local key = "plr-"..plr.UserId
local savedData
local success, err = pcall(function()
savedData = dataStore1:GetAsync(key)
end)
if not success then
warn("Failed to save data: "..tostring(err))
return
end
if savedData then
cash.Value = savedData.Cash
lvl.Value = savedData.Level
xp.Value = savedData.XP
xpNeeded2 = savedData.RequireXP
else
Save(plr)
end
end
local function gainXP(plr)
print(xpNeeded)
local stats = plr:WaitForChild("leaderstats")
local lvl = stats.Level
local xp = stats.XP
local xpNeeded = 350
while wait(5) do
xp.Value = xp.Value + 250
if xp.Value >= xpNeeded then
lvl.Value = lvl.Value + 1
xp.Value = 0
xpNeeded = xpNeeded * math.sqrt(5 * (math.random(1.5, 2)) ) --do some multiplying
local divided = xpNeeded / 5
local rounded = 5 * math.floor(divided)
xpNeeded = rounded
print(xpNeeded)
end
end
end
players.PlayerAdded:Connect(Load)
wait(1)
players.PlayerAdded:Connect(gainXP)
players.PlayerRemoving:Connect(Save)
My original script:
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore1 = dataStoreService:GetDataStore(“SaveCashDataII”)
local function Save(plr)
local key = "plr-"..plr.UserId
local save = {
["Cash"] = plr.leaderstats.Cash.Value,
["Level"] = plr.leaderstats.Level.Value,
["XP"] = plr.leaderstats.XP.Value
}
local success, err = pcall(function()
dataStore1:SetAsync(key, save)
end)
if not success then
warn("Failed to overwrite data: "..tostring(err))
end
end
local function Load(plr)
local xpNeeded2
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 500
cash.Parent = leaderstats
local lvl = Instance.new("NumberValue")
lvl.Name = "Level"
lvl.Value = 1
lvl.Parent = leaderstats
local xp = Instance.new("NumberValue")
xp.Name = "XP"
xp.Value = 0
xp.Parent = leaderstats
local key = "plr-"..plr.UserId
local savedData
local success, err = pcall(function()
savedData = dataStore1:GetAsync(key)
end)
if not success then
warn("Failed to save data: "..tostring(err))
return
end
if savedData then
cash.Value = savedData.Cash
lvl.Value = savedData.Level
xp.Value = savedData.XP
else
Save(plr)
end
end
local function gainXP(plr)
local stats = plr:WaitForChild("leaderstats")
local lvl = stats.Level
local xp = stats.XP
local xpNeeded = 350
while wait(5) do
xp.Value = xp.Value + 250
if xp.Value >= xpNeeded then
lvl.Value = lvl.Value + 1
xp.Value = 0
xpNeeded = xpNeeded * math.sqrt(5 * (math.random(1.5, 2)) ) --do some multiplying
local divided = xpNeeded / 5
local rounded = 5 * math.floor(divided)
xpNeeded = rounded
print(xpNeeded)
end
end
end
players.PlayerAdded:Connect(Load)
wait(1)
players.PlayerAdded:Connect(gainXP)
players.PlayerRemoving:Connect(Save)
Um, your save script seems to already save your stats in a table. Do you mean, a general server variable? You can keep that in ServerStorage and it will load for all servers… If you mean saving a player variable, you have already done that. I’m not sure what you’re asking, I feel like I am answering two questions at the same time. Could you elaborate on what you mean?
If it is just a constant, why do you need to save it? It can just be stated in a script and it will load when you play the game.
If your xpNeeded is dynamic, then perhaps you have some equation modelling what it should be for certain levels? In that case, you don’t need to save it, you just need to call the function that calculates what xpNeeded should be.
Again, it is hard to answer your question because you are just giving me one-liners without much background info… But hopefully I’ve covered what you are looking for.
I will put all my code here once again:
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore1 = dataStoreService:GetDataStore(“SaveCashDataI”)
local function Save(plr)
local key = "plr-"..plr.UserId
local save = {
["Cash"] = plr.leaderstats.Cash.Value,
["Level"] = plr.leaderstats.Level.Value,
["XP"] = plr.leaderstats.XP.Value
}
local success, err = pcall(function()
dataStore1:SetAsync(key, save)
end)
if not success then
warn("Failed to overwrite data: "..tostring(err))
end
end
local function Load(plr)
local xpNeeded2
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 500
cash.Parent = leaderstats
local lvl = Instance.new("NumberValue")
lvl.Name = "Level"
lvl.Value = 1
lvl.Parent = leaderstats
local xp = Instance.new("NumberValue")
xp.Name = "XP"
xp.Value = 0
xp.Parent = leaderstats
local key = "plr-"..plr.UserId
local savedData
local success, err = pcall(function()
savedData = dataStore1:GetAsync(key)
end)
if not success then
warn("Failed to save data: "..tostring(err))
return
end
if savedData then
cash.Value = savedData.Cash
lvl.Value = savedData.Level
xp.Value = savedData.XP
else
Save(plr)
end
end
local function GetXP(level)
if level <= 1 then
local RequireXP = 350
return RequireXP
elseif level > 1 then
local RequireXP = 350 * level * math.sqrt(4 * (math.random(1.5, 2)) )
RequireXP = 5 * math.floor(RequireXP / 5)
return RequireXP
end
end
local function gainXP(plr)
local stats = plr:WaitForChild("leaderstats")
local lvl = stats.Level.Value
local xp = stats.XP
local xpNeeded = GetXP(lvl)
print(xpNeeded)
while wait(5) do
xp.Value = xp.Value + 250
if xp.Value >= xpNeeded then
lvl = lvl + 1
xp.Value = 0
xpNeeded = GetXP(lvl)
print(xpNeeded)
end
end
end
players.PlayerAdded:Connect(Load)
wait(0.5)
players.PlayerAdded:Connect(gainXP)
players.PlayerRemoving:Connect(Save)
There is something still wrong. I don’t think it saves still.
Lol, I was looking through my old topics and came across this again. I soon realized I would have to make a whole new datastore. I’ve done this before so I shouldn’t have a problem doing it again!