Hi im not really good with data store i cant find any problem in this script
please help me find the problem
local dataservice = game:GetService("DataStoreService")
local mydata = dataservice:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Finish = Instance.new("IntValue")
Finish.Parent = plr
Finish.Name = "Finish"
local MazeBux = Instance.new("IntValue")
MazeBux.Parent = leaderstats
MazeBux.Name = "MazeBux"
local Rank = Instance.new("StringValue")
Rank.Parent = leaderstats
Rank.Name = "Rank"
Rank.Value = "Maze Noobie"
local plrId = plr.UserId
local data
local s, e pcall(function()
data = mydata:GetAsync(plrId)
end)
if s then
Rank.Value = data.Rank
MazeBux.Value = data.Bux
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local dataToSave = {
Rank = plr.leaderstats.Rank.Value;
Bux = plr.leaderstats.MazeBux.Value
}
print(dataToSave.Rank)
print(dataToSave.Bux)
local plrId = plr.UserId
print(plrId)
local s, e pcall(function()
mydata:SetAsync(plr.UserId, dataToSave)
end)
if s then
print(s)
print("Data SuccessFully saved")
else
print(e)
print("Failed")
end
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetChildren())do
local dataToSave = {
Rank = plr.leaderstats.Rank.Value;
Bux = plr.leaderstats.MazeBux.Value
}
local plrId = plr.UserId
print(plrId)
local s, e pcall(function()
mydata:SetAsync(plrId, dataToSave)
end)
if s then
print("Data SuccessFully saved")
else
print("Failed")
end
end
end)