Basically I want this script to save my “Money$” currency for when the players leaves and joins back it will save. for example currency = 5, player leaves and joins back with currency equaling to 5, their currency saves.
my script: (Please help me modify is to make it work)
local DataStoreService = game:GetService("DataStoreService")
local dataBase = DataStoreService:GetDataStore("data")
local SessionData = {}
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new('Folder')
leaderstats.Parent = plr
leaderstats.Name = "leaderstats"
local Money = Instance.new('IntValue')
Money.Parent = leaderstats
Money.Name = "Money$"
Money.Value = 0
local success = nil
local PlayerData = nil
local attempt = 1
repeat
success, PlayerData = pcall(function()
return dataBase:GetAsync(plr.UserId)
end)
attempt += 1
if not success then
warn(PlayerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected to database")
if not PlayerData then
print("Assigning default data")
PlayerData = {
["Money$"] = 0
}
end
SessionData[plr.UserId] = PlayerData
else
warn("Unable to get data for", plr.UserId)
plr:Kick("Unable to load your data. Please try again later, Thanks!")
end
Money.Value = SessionData[plr.UserId].Money
Money.Changed:Connect(function()
SessionData[plr.UserId].Money = Money.Value
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
if SessionData[plr.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
dataBase:GetAsync(plr.UserId, SessionData[plr.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data Saved For", plr.Name)
else
warn("Unable To Save For", plr.Name)
end
end
end)
That’s fine, but what surprises me the most is that you get to a topic and say the simplest solution, but you don’t do your best… of course, you get to the easy-to-solve issues and you give the answer, because as always you’re in the forum. … hey @Yo2uber99 what this @DasKairo said is fine, but when you want to save more things in your game it will become saturated, so I recommend that you use: DataStore2 to save values is better for saving values since its saving method is much safer and avoids data loss in a more efficient way. not like they said before
Don’t worry, I understand you, it’s what happens when you get to simple topics, which is what you always do, but @Yo2uber99 I recommend that you watch the tutorial well, so that you understand it well and so your game will be very safe and with a good database
you arent really making a good argument either, DataStore2 may help, but its still a “simple answer” plus, It isn’t that cool to tell a person with a Already good enough System to use another.
I already went through that… I made a good system but in the end when I wanted to add more things to my game the data loss came… and when a game has many constant visits is when one as a developer has to be in action but sometimes it is better to prepare beforehand with DataStore2. in summary:
It is not necessary to change it in such a way but it would help you much easier to make a safer system without errors and the best of all is that it does not take so much work and it will teach you more things like UpdateAsyncalthough I would say that this would be for tables
Ignoring the commotion from above and only focusing on the post that sums up what you’re doing wrong:
As @DasKairo had stated, in the Players.PlayerRemoving event, you’re using GetAsync. You should’ve been using SetAsync if you’re trying to save the data when the player leaves (which, you’re supposed to).
And when you’re saving the data, you don’t need to use attempts, either. So, the best way you’d go at this is just this code block:
local success, errorMsg = pcall(function()
dataBase:SetAsync(plr.UserId, SessionData[plr.UserId])
end)
if success then
print("Saved successfully!")
else
warn(errorMsg)
end
Not to mark my post as a solution, though. Again, the first reply to your topic deserves it.
Sir, I’m not exactly sure if someone pissed you off or what not. But, I’m simply just stating what their problem is and what they should do to fix it. Anything after that, referring to me saying that they “don’t need to use attempts”, is strictly my opinion. His way of doing it is just fine as is; he doesn’t have to rewrite his script, as it’s unnecessary at this time. Thank you for understanding.
Thank you for your “Helping” but I don’t think you need to be rude to others trying to help me… I am just trying to figure out why my damn script wouldn’t work because roblox studios doesn’t appear to like me most the time, I searched up a lot of different videos and even looked a bit into the dev thing on roblox studio to learn what each does. Most videos are outdated since a lot of updates, So i’m just trying to get this done and out of the way since I have other scripting I want to do. I know quite a lot coding but just certain parts I don’t really know as well. Not trying to be rude or anything but just don’t need to get mad at others.