To make my data store because I have no clue how they actually work. But when I finished it seemed that everything was right. I allowed API access, I tested it in Roblox studio, and even Roblox when the game was private and public. It does not work, it could be me but I read the replies to the tutorial and a lot of people were complaining. Is there anyone that can help me fix this issue?
Also, no errors but it prints “Data not saved” from a print in the script.
Script:
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Coins.Value ,
player.GoldenHook.Owned.Value,
player.TPose.Owned.Value,
player.Tpose.Equipped.Value,
player.GoldenHook.Equipped.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
local Coins = Instance.new("IntValue")
local GoldenHook = Instance.new("Folder")
local OwnedGH = Instance.new("BoolValue")
local TPose = Instance.new("Folder")
local OwnedTP = Instance.new("BoolValue")
local EquippedTP = Instance.new("BoolValue")
local EquippedGH = Instance.new("BoolValue")
--//Coins//--
leaderstats.Name = "leaderstats"
Coins.Name = "Coins"
leaderstats.Parent = player
Coins.Parent = leaderstats
--//Golden Hook//--
GoldenHook.Name = "GoldenHook"
OwnedGH.Name = "Owned"
GoldenHook.Parent = player
OwnedGH.Parent = GoldenHook
--//TPose//--
TPose.Name = "TPose"
OwnedTP.Name = "Owned"
TPose.Parent = player
OwnedTP.Parent = TPose
--//TP Equip//--
EquippedTP.Name = "Equipped"
EquippedTP.Parent = TPose
--//GH Equip//--
EquippedGH.Name = "Equipped"
EquippedGH.Parent = GoldenHook
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data[1]
OwnedGH.Value = data[2]
OwnedTP.Value = data[3]
EquippedTP.Value = data[4]
EquippedGH.Value = data[5]
else
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerAdded:Connect(function(Plr)
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = Plr
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
end)
-- // Assigning variables //
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Coins.Value ,
player.GoldenHook.Owned.Value,
player.TPose.Owned.Value,
player.Tpose.Equipped.Value,
player.GoldenHook.Equipped.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
wait()
local plrid = "id_"..player.UserId
local leaderstats = Instance.new("Folder")
local Coins = Instance.new("IntValue")
local GoldenHook = Instance.new("Folder")
local OwnedGH = Instance.new("BoolValue")
local TPose = Instance.new("Folder")
local OwnedTP = Instance.new("BoolValue")
local EquippedTP = Instance.new("BoolValue")
local EquippedGH = Instance.new("BoolValue")
--//Coins//--
leaderstats.Name = "leaderstats"
Coins.Name = "Coins"
leaderstats.Parent = player
Coins.Parent = leaderstats
--//Golden Hook//--
GoldenHook.Name = "GoldenHook"
OwnedGH.Name = "Owned"
GoldenHook.Parent = player
OwnedGH.Parent = GoldenHook
--//TPose//--
TPose.Name = "TPose"
OwnedTP.Name = "Owned"
TPose.Parent = player
OwnedTP.Parent = TPose
--//TP Equip//--
EquippedTP.Name = "Equipped"
EquippedTP.Parent = TPose
--//GH Equip//--
EquippedGH.Name = "Equipped"
EquippedGH.Parent = GoldenHook
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data[1]
OwnedGH.Value = data[2]
OwnedTP.Value = data[3]
EquippedTP.Value = data[4]
EquippedGH.Value = data[5]
else
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerAdded:Connect(function(Plr)
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = Plr
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
end)
-- // Assigning variables //
game.Players.PlayerRemoving:Connect(function(plr)
dataStore:SetAsync("id_"..plr.UserId, {
plr.leaderstats.Coins.Value ,
plr.GoldenHook.Owned.Value,
plr.TPose.Owned.Value,
plr.TPose.Equipped.Value,
plr.GoldenHook.Equipped.Value
}) -- When a player leaves
local success, err = pcall(function()
saveData(plr) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
game.Players.PlayerAdded:Connect(function(player)
wait()
local plrid = "id_"..player.UserId
local tableToSave = {
player.leaderstats.Coins.Value ,
player.GoldenHook.Owned.Value,
player.TPose.Owned.Value,
player.Tpose.Equipped.Value,
player.GoldenHook.Equipped.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
local leaderstats = Instance.new("Folder")
local Coins = Instance.new("IntValue")
local GoldenHook = Instance.new("Folder")
local OwnedGH = Instance.new("BoolValue")
local TPose = Instance.new("Folder")
local OwnedTP = Instance.new("BoolValue")
local EquippedTP = Instance.new("BoolValue")
local EquippedGH = Instance.new("BoolValue")
--//Coins//--
leaderstats.Name = "leaderstats"
Coins.Name = "Coins"
leaderstats.Parent = player
Coins.Parent = leaderstats
--//Golden Hook//--
GoldenHook.Name = "GoldenHook"
OwnedGH.Name = "Owned"
GoldenHook.Parent = player
OwnedGH.Parent = GoldenHook
--//TPose//--
TPose.Name = "TPose"
OwnedTP.Name = "Owned"
TPose.Parent = player
OwnedTP.Parent = TPose
--//TP Equip//--
EquippedTP.Name = "Equipped"
EquippedTP.Parent = TPose
--//GH Equip//--
EquippedGH.Name = "Equipped"
EquippedGH.Parent = GoldenHook
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data[1]
OwnedGH.Value = data[2]
OwnedTP.Value = data[3]
EquippedTP.Value = data[4]
EquippedGH.Value = data[5]
else
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerAdded:Connect(function(Plr)
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = Plr
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
end)
-- // Assigning variables //
game.Players.PlayerRemoving:Connect(function(plr)
dataStore:SetAsync("id_"..plr.UserId, {
plr.leaderstats.Coins.Value ,
plr.GoldenHook.Owned.Value,
plr.TPose.Owned.Value,
plr.TPose.Equipped.Value,
plr.GoldenHook.Equipped.Value
}) -- When a player leaves
local success, err = pcall(function()
saveData(plr) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)
I’m guessing these are tools. So wouldn’t it be plr.tools.GoldenHook.Owned and so on for the rest, it will only be .value for stats I think. Don’t make anything changes for this until you get some actual programmer to help you, as for i am not a programmer.
Oh dont worry, those are bool values, I just give them very weird names. I understand it so it works for me, also I will try out the script you gave me. If It does not work I think I might know a few legit tutorials.
Check your api settings for studio, I’ll read through your code more thoroughly but at the moment it looks fine.
Edit: you call pcall twice when saving data, once in the function, then again when you call it. Because of this, the one outside of your save function will printout data not saved because you never return true or false in the function.
Followed a DIFFERENT tutorial, but it still does not work. Seems like this one is better. Can you check it out?
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local dataStore = DataStoreService:GetDataStore("Statistics")
local function waitForRequestBudget(requestType)
local CurrentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while CurrentBudget < 1 do
CurrentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
wait(5)
end
end
local function save(player, dontWait)
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = player:FindFirstChild("leaderstats")
local GoldenHook = player:FindFirstChild("GoldenHook")
local TPose = player:FindFirstChild("TPose")
local statistics = {
leaderstats.Coins.Value,
GoldenHook.Owned.Value,
GoldenHook.Equipped.Value,
TPose.Owned.Value,
TPose.Equipped.Value
}
local success, ret
repeat
if not dontWait then
waitForRequestBudget(Enum.DataStoreRequestType.SetIncrementAsync)
end
success, ret = pcall(dataStore.UpdateAsync, dataStore, key, function()
return statistics
end)
until success
end
local function SetUp(player)
local userId = player.UserId
local key = "Player_" .. userId
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
local GoldenHook = Instance.new("Folder")
GoldenHook.Name = "GoldenHook"
local OwnedGH = Instance.new("BoolValue")
OwnedGH.Name = "Owned"
local TPose = Instance.new("Folder")
TPose.Name = "TPose"
local OwnedTP = Instance.new("BoolValue")
OwnedTP.Name = "Owned"
local EquippedTP = Instance.new("BoolValue")
EquippedTP.Name = "Equipped"
local EquippedGH = Instance.new("BoolValue")
EquippedGH.Name = "Equipped"
local name = player.Name
local success, ret
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, ret = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(name)
if success then
Coins.Value = ret or 0
OwnedGH.Value = ret or false
OwnedTP.Value = ret or false
EquippedTP.Value = ret or false
EquippedGH.Value = ret or false
leaderstats.Parent = player
Coins.Parent = leaderstats
GoldenHook.Parent = player
OwnedGH.Parent = GoldenHook
TPose.Parent = player
OwnedTP.Parent = TPose
EquippedTP.Parent = TPose
EquippedTP.Parent = TPose
EquippedGH.Parent = GoldenHook
else
print("There was an error " .. ret)
end
end
local RunService = game:GetService("RunService")
local function onShutDown()
if RunService:IsStudio() then
wait(2)
else
local finished = Instance.new("BindableEvent")
local allPlayers = Players:GetPlayers()
local leftPlayers = #allPlayers
for _,player in ipairs(allPlayers) do
coroutine.wrap(function()
save(player)
leftPlayers -= 1
if leftPlayers == 0 then
finished:Fire()
end
end)()
end
finished.Event:Wait()
end
end
Players.PlayerAdded:Connect(SetUp)
Players.PlayerRemoving:Connect(save)
game.Players.PlayerAdded:Connect(function(Plr)
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = Plr
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
end)
Just because the script is longer doesn’t mean it’s better, it usually means the opposite. This script is missing the component that saves the data when the game is restarted and also misses some other parts.
Your original script also has some problems, especially with the way you’re using your save function. The save function already has it’s own error handling, but for some reason, you put it around a pcall, which would silence any errors. You also could’ve put the second .PlayerAdded function into the first one.
Code:
--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
--//Variables
local dataStore = DataStoreService:GetDataStore("MyDataStore")
--//Functions
local function saveData(player)
local tableToSave = {
Coins = player.leaderstats.Coins.Value,
OwnedGH = player.GoldenHook.Owned.Value,
OwnedTP = player.TPose.Owned.Value,
EquippedTP = player.Tpose.Equipped.Value,
EquippedGH = player.GoldenHook.Equipped.Value
}
local success, errorMessage = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
--//Coins//--
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
--//Golden Hook//--
local GoldenHook = Instance.new("Folder")
GoldenHook.Name = "GoldenHook"
GoldenHook.Parent = player
local OwnedGH = Instance.new("BoolValue")
OwnedGH.Name = "Owned"
OwnedGH.Parent = GoldenHook
--//TPose//--
local TPose = Instance.new("Folder")
TPose.Name = "TPose"
TPose.Parent = player
local OwnedTP = Instance.new("BoolValue")
OwnedTP.Name = "Owned"
OwnedTP.Parent = TPose
--//TP Equip//--
local EquippedTP = Instance.new("BoolValue")
EquippedTP.Name = "Equipped"
EquippedTP.Parent = TPose
--//GH Equip//--
local EquippedGH = Instance.new("BoolValue")
EquippedGH.Name = "Equipped"
EquippedGH.Parent = GoldenHook
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = player
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
local data = nil
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data.Coins
OwnedGH.Value = data.OwnedGH
OwnedTP.Value = data.OwnedTP
EquippedTP.Value = data.EquippedTP
EquippedGH.Value = data.EquippedGH
else
print("The player has no data!") -- The default will be set to 0
end
end)
Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function() -- When the server shuts down
for _, player in Players:GetPlayers() do -- Loop through all the players
saveData(player)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore")
local function saveData(player)
local tableToSave = {
player.leaderstats.Coins.Value,
player.GoldenHook.Owned.Value,
player.TPose.Owned.Value,
player.TPose.Equipped.Value,
player.GoldenHook.Equipped.Value
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave)
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
local Coins = Instance.new("IntValue")
local GoldenHook = Instance.new("Folder")
local OwnedGH = Instance.new("BoolValue")
local TPose = Instance.new("Folder")
local OwnedTP = Instance.new("BoolValue")
local EquippedTP = Instance.new("BoolValue")
local EquippedGH = Instance.new("BoolValue")
--//Coins//--
leaderstats.Name = "leaderstats"
Coins.Name = "Coins"
leaderstats.Parent = player
Coins.Parent = leaderstats
--//Golden Hook//--
GoldenHook.Name = "GoldenHook"
OwnedGH.Name = "Owned"
GoldenHook.Parent = player
OwnedGH.Parent = GoldenHook
--//TPose//--
TPose.Name = "TPose"
OwnedTP.Name = "Owned"
TPose.Parent = player
OwnedTP.Parent = TPose
--//TP Equip//--
EquippedTP.Name = "Equipped"
EquippedTP.Parent = TPose
--//GH Equip//--
EquippedGH.Name = "Equipped"
EquippedGH.Parent = GoldenHook
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Coins.Value = data[1]
OwnedGH.Value = data[2]
OwnedTP.Value = data[3]
EquippedTP.Value = data[4]
EquippedGH.Value = data[5]
else
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerAdded:Connect(function(Plr)
local Folder4 = Instance.new("Folder")
Folder4.Name = "NumberOfHooks"
Folder4.Parent = Plr
local IntValue2 = Instance.new("IntValue")
IntValue2.Name = "Amount"
IntValue2.Parent = Folder4
end)
-- // Assigning variables //
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)