Hey guys, I’ve been working on a aging system which requires me to save multiple data (age and seconds) I’ve tried so many things and many posts I’ve seen were not including pcalls, I’m really new to this datastore stuff so I’d appeciate if you can help me and explain why my script isn’t working/ how your solution works.
Also I’ve watched some tutorials and the dude was using module scripts (and I don’t know the reason) so maybe that’s why it was messing up?
I have 15 different versions of this script but I think this is the closest I got?
local module = {}
local DataStoreService = game:GetService("DataStoreService")
local ageDataStore = DataStoreService:GetDataStore("ageDataStore")
game.Players.PlayerAdded:Connect(function(player)
local Stats = Instance.new("Folder")
Stats.Name = "Stats"
Stats.Parent = player
local age = Instance.new("IntValue")
age.Name = "Age"
age.Parent = Stats
local seconds = Instance.new("IntValue")
seconds.Name = "Seconds"
seconds.Parent = age
while wait(1) do
seconds.Value = seconds.Value + 1
if seconds.Value >= 3600
then
age.Value = age.Value + 1
seconds.Value = 0
end
end
local data
local success, errormessage = pcall(function()
data = ageDataStore:GetAsync(player.UserId.."-age","-seconds")
end)
if success and data then
wait(2)
player.Stats.Age.Value = data.age
player.Stats.Age.Seconds.Value = data.seconds
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {}
local success, errormessage = pcall(function()
ageDataStore:SetAsync(player.UserId.."-age","-seconds")
end)
if success then
data.age = player.Stats.Age.Value
data.seconds = player.Stats.Age.Seconds.Value
print ("Saved Data Successfully!")
else
print("Couldn't Save Data!")
warn(errormessage)
end
end)
return module