HI I am busy with a datastore but I cant seem to get it working. It worked in the past before, It seems like it may be the bindtoclose.
Can Anyone help me to fix it? Please
Leaderstats - Normal Script
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local plrDataStore = require(script.Parent.plrDataStore)
local function onPlayerJoin(player)
local loadedData = plrDataStore.getDataForPlayer(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
Clicks.Value = loadedData
local Rebirths = Instance.new("IntValue")
Rebirths.Name = "Rebirths"
Rebirths.Parent = leaderstats
Rebirths.Value = loadedData
local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Parent = leaderstats
Gems.Value = loadedData
end
local function onPlayerExit(player)
local PlayerStats = player:FindFirstChild("leaderstats")
local saveData = {
Clicks = PlayerStats.Clicks.Value,
Rebirths = PlayerStats.Rebirths.Value,
Gems = PlayerStats.Gems.Value
}
plrDataStore.saveDataForPlayer(player, saveData)
end
game:BindToClose(function(player)
local PlayerStats = player:FindFirstChild("leaderstats")
for i,v in pairs(Players:GetPlayers()) do
local saveData = {
Clicks = PlayerStats.Clicks.Value,
Rebirths = PlayerStats.Rebirths.Value,
Gems = PlayerStats.Gems.Value
}
wait(5)
plrDataStore.saveDataForPlayer(player, saveData)
end
end)
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
DataStore - Module Script
local DataStoreService = game:GetService("DataStoreService")
local PlayerStore = DataStoreService:GetDataStore("StoreTest")
local DefaultData = {
Clicks = 0,
Rebirths = 0,
Gems = 0
}
local PlayerDataStore = {}
function PlayerDataStore.getDataForPlayer(player)
local PlayerData
local Success, err = pcall(function()
PlayerData = PlayerStore:GetAsync(player.UserId)
end)
if not Success then
print(err)
PlayerData = DefaultData
else
print("Found Data")
end
return PlayerData
end
function PlayerDataStore.saveDataForPlayer(player, saveData)
local success, err = pcall(function()
PlayerStore:SetAsync(player.UserId, saveData)
end)
if not success then
print(err)
print("Something went wrong cant save data!")
end
end
return PlayerDataStore
Thank You! Much appreciated.