I don’t understand why this script is not working. It gets stuck when it’s meant to clone a file to the player containing stats (Cash) and the strange part is at it comes up with no errors despite the face it doesn’t do what the script tells it to do. Please help
local DataStoreService = game:GetService(“DataStoreService”)
local dataStore = DataStoreService:GetDataStore(“PlayerData2”)
local updateWallet = game:GetService(“ReplicatedStorage”):WaitForChild(“UpdateWallet”)
local loaded = {}
game.Players.PlayerAdded:Connect(function(player)
local playerstatistics = game:GetService(“ServerStorage”):WaitForChild(“Stats”)
local success, value = pcall(dataStore.GetAsync, dataStore, player.UserId)
if success == false then player:Kick(“Datastore failed to load”) return end
local data = value or {}
print(“Loaded:”, data)
for i, folder in playerstatistics:GetChildren() do
local subData = data[folder.Name] or {}
for i, child in folder:Clone():GetChildren() do
child.Value = subData[child.Name] or child.Value
folder:Clone().Parent = game.Players.LocalPlayer
end
folder:Clone().Parent = game.Players.LocalPlayer
end
loaded[player] = true
end)
game.Players.PlayerRemoving:Connect(function(player)
if loaded[player] == nil then return end
local data = {}
for i, folder in game:GetService(“ServerStorage”):WaitForChild(“Stats”):GetChildren() do
local subData = {}
for i, child in player[folder.Name]:GetChildren() do
subData[child.Name] = child.Value
end
data[folder.Name] = subData
folder:Destroy()
end
local success, value = pcall(dataStore.SetAsync, dataStore, player.UserId, data)
print(“Saved:”, data)
loaded[player] = nil
dataStore:SetAsync(player.UserId,player.satistic.Cash.Value)
print(“Saved!”)
end)
game:BindToClose(function()
while next(loaded) ~= nil do
task.wait()
end
end)