is something wrong with my datastore because its not working? [closed]
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Datastored")
local tries = 3
local dataLoaded = false
local function Set(plr)
if dataLoaded then
local key ="plr-" .. plr.UserId
local count = 0
local data = {
["Clicks"] = plr.leaderstats.Click.Value,
["Prestiges"] = plr.leaderstats.Prestiges.Value,
["Coins"] = plr.leaderstats.Coins.Value,
["Class"] = plr.leaderstats.Class.Value
}
local success, err
repeat
success, err = pcall(function()
dataStore:SetAsync(key, data)
end)
count = count + 1
until count >= tries or success
if not success then
warn("Failed to set data. Error code: " .. tostring(err))
return
end
else
return
end
end
local function Get(plr)
local key ="plr-" .. plr.UserId
local count = 0
local data
local success, err
repeat
success, err = pcall(function()
data = dataStore:GetAsync(key)
end)
count = count + 1
until count >= tries or success
if not success then
warn("Failed to read data. Error code: " .. tostring(err))
plr:Kick("You have been kicked to save your data. Please Rejoin")
return
end
if success then
if data then
return data
else
return {
["Clicks"] = 0,
["Prestiges"] = 0,
["Coins"] = 0,
["Class"] = 0
}
end
end
end
local function createLeaderstats(plr)
local values = Get(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = leaderstats
local Prestiges = Instance.new("IntValue")
Prestiges.Name = "Prestiges"
Prestiges.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local Class = Instance.new("IntValue")
Class.Name = "Class"
Class.Parent = leaderstats
Clicks.Value = values.Clicks
Prestiges.Value = values.Prestiges
Coins.Value = values.Coins
Class.Value = values.Class
dataLoaded = true
end
players.PlayerRemoving:Connect(Set)
players.PlayerAdded:Connect(createLeaderstats)
game:BindToClose(function()
for i, v in next, game.Players:GetChildren() do
if v then
Set(v)
end
end
end)
Closed as Non-Descriptive by JesseSong
This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.
Why was this question closed?