Alright, ill try to explain whats happening as clearly as possible.
So I have a couple of parts in my workspace, which have a few attributes. My aim is to save the values of these attributes.
Now, ive got the script done (at least i think so)
Although, in the player removing function the pcall function doesnt seem to run, and in the :BindToClose function, the script doesnt seem to continue after a loop
My code:
local DataStoreService = game:GetService("DataStoreService")
local gridDataStore = DataStoreService:GetDataStore("GridDataStore")
game.Players.PlayerAdded:Connect(function(player)
local GridFolder = game.Workspace.Grid
local key = player.UserId
local data
local success,errormessage = pcall(function()
print('adding pcall')
data = gridDataStore:GetAsync(key)
end)
if success then
print("success loading the data")
if data~= nil then
print("data is not nil")
print(data)
local index = 0
for i,v in pairs(data) do
index += 1
GridFolder[tostring(index)]:SetAttribute("BuildingName", data[tostring(index)]["BuildingName"])
GridFolder[tostring(index)]:SetAttribute("BuildingStatus", data[tostring(index)]["BuildingStatus"])
GridFolder[tostring(index)]:SetAttribute("Floors", data[tostring(index)]["Floors"])
GridFolder[tostring(index)]:SetAttribute("Income", data[tostring(index)]["Income"])
end
end
else
print(errormessage)
end
-- getasync
end)
game.Players.PlayerRemoving:Connect(function(player)
--setasync
print("player removing")
local data = {}
local GridFolder = game.Workspace.Grid
for i,v in pairs(GridFolder:GetChildren()) do
if v.ClassName == 'Script' then return end
local index = tonumber(v.Name)
local t = {
["BuildingName"] = v:GetAttribute("BuildingName"),
["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
["Floors"] = v:GetAttribute("Floors"),
["Income"] = v:GetAttribute("Income")
}
data[tostring(index)] = t
end
local success,errormessage = pcall(function()
print('removing pcall')
gridDataStore:SetAsync(player.UserId,data)
end)
if success then
print("Data saved")
else
warn("DATA NOT SAVED"..errormessage)
end
end)
game:BindToClose(function()
for k,player in pairs(game.Players:GetPlayers()) do
print('game closing')
local data = {}
local GridFolder = game.Workspace.Grid
for i,v in pairs(GridFolder:GetChildren()) do
if v.ClassName == 'Script' then return end
local index = tonumber(v.Name)
local t = {
["BuildingName"] = v:GetAttribute("BuildingName"),
["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
["Floors"] = v:GetAttribute("Floors"),
["Income"] = v:GetAttribute("Income")
}
data[tostring(index)] = t
end
print('removing pcall')
gridDataStore:SetAsync(player.UserId,data)
end
end)
the “removing pcall” print statement never prints in the player removing function or bind to close. And the data isnt being saved either.
Also ive tried testing it in game in roblox, in studio as well. I’ve turned on all the necessary toggles in settings-security as well
All help is appreciated.