Hi this is my datastore script: I need help with pcalls and bindtoclose. I am completely stuck on where to put them in my script and Ive been getting a lot of issues.
local PSDataStore = game:GetService("DataStoreService"):GetDataStore("APBodyAcc")
local Stats = script.BodyAcc
local StatsHolderName = "BodyAcc"
local StatsHolderType = "Folder"
game.Players.PlayerAdded:connect(function(NewPlayer)
local StatsHolder = Instance.new(StatsHolderType, NewPlayer)
StatsHolder.Name = StatsHolderName
local Key = "BodyAcc-" .. NewPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
local GetSave
local success, response = pcall(function()
GetSave = PSDataStore:GetAsync(Key)
end)
for _, Stat in pairs(Stats:GetChildren()) do
local NewStats = Instance.new(Stat.ClassName, StatsHolder)
NewStats.Name = Stat.Name
NewStats.Value = Stat.Value
end
if GetSave then --Load Data
for _n, ls in pairs(StatsHolder:GetChildren()) do
ls.Value = GetSave[_n]
end
else --Saves data!
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Created New DataStore!")
PSDataStore:SetAsync(Key, StatsToSave)
end
while wait(300) do
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end
end)
game.Players.PlayerRemoving:connect(function(OldPlayer)
local Key = "BodyAcc-" .. OldPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
local StatsToSave = {}
for _n, ss in pairs(OldPlayer[StatsHolderName]:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
local GetSave
local success, response = pcall(function()
GetSave = PSDataStore:GetAsync(Key, StatsToSave)
end)
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end)