local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local data = Datastore:GetDataStore("data")
game.Players.PlayerAdded:Connect(function(player)
--creating values
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
local checkpoints1 = Instance.new("IntValue", leadstats)
checkpoints1.Name = "Checkpointch1"
local temp = Instance.new("IntValue", leadstats)
temp.Name = "temp"
local chapters = Instance.new("IntValue", leadstats)
chapters.Name = "chapters"
--values
local playerUserId = "Player_"..player.UserId
leadstats.Parent = player
local success,save = pcall(function()
return data:GetAsync(playerUserId)
end)
if success then
-- Setting saved data to player
checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true)
else
repeat checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true) until success
local webhookdata = {["embeds"] = {{
["title"] = "Error",
["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
local webhookmsg = https:JSONEncode(webhookdata)
task.wait(30)
https:PostAsync(url, webhookmsg)
warn(save)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
--saving
local playerUserId = "Player_"..player.UserId
-- Player int values
local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")
local success, errormsg
success, errormsg = pcall(function()
local save
--table
if script:GetAttribute("Loaded" == true) then
local savedata = {}
savedata['chapters'] = chapters.Value
savedata['checkpoints1'] = checkpoints1.Value
print(savedata)
save = data:SetAsync(playerUserId, savedata)
end
end)
if success then
print("Data saved")
else
local webhookdata = {
["embeds"] = {
{
["title"] = "Error",
["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
}
}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url, webhookmsg)
warn(errormsg)
end
end)
it either doesnât save or load.
i tried using bindtoclose but it didnât work.
What exactly is the problem? All youâve given us is the block of code so we have nothing to work with. We donât understand what the bug is. Could you explain a bit more?
The script overall seems normal but I adjusted it slightly and removed some lines that were unnecessary.
local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data
game.Players.PlayerAdded:Connect(function(player)
--creating values
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
local checkpoints1 = Instance.new("IntValue", leadstats)
checkpoints1.Name = "Checkpointch1"
local temp = Instance.new("IntValue", leadstats)
temp.Name = "temp"
local chapters = Instance.new("IntValue", leadstats)
chapters.Name = "chapters"
--values
local playerUserId = "Player_"..player.UserId
leadstats.Parent = player
local success,save = pcall(function()
data = datastore:GetAsync(playerUserId)
end)
if success then
-- Setting saved data to player
checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true)
else
repeat checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true) until success
local webhookdata = {["embeds"] = {{
["title"] = "Error",
["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
local webhookmsg = https:JSONEncode(webhookdata)
task.wait(30)
https:PostAsync(url, webhookmsg)
warn(save)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
--saving
local playerUserId = "Player_"..player.UserId
-- Player int values
local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")
local success, errormsg = pcall(function()
local save
--table
if script:GetAttribute("Loaded" == true) then
local savedata = {}
savedata['chapters'] = chapters.Value
savedata['checkpoints1'] = checkpoints1.Value
print(savedata)
save = data:SetAsync(playerUserId, savedata)
end
end)
if success then
print("Data saved")
else
local webhookdata = {
["embeds"] = {
{
["title"] = "Error",
["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
}
}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url, webhookmsg)
warn(errormsg)
end
end)
local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data
game.Players.PlayerAdded:Connect(function(player)
--creating values
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
local checkpoints1 = Instance.new("IntValue", leadstats)
checkpoints1.Name = "Checkpointch1"
local temp = Instance.new("IntValue", leadstats)
temp.Name = "temp"
local chapters = Instance.new("IntValue", leadstats)
chapters.Name = "chapters"
--values
local playerUserId = "Player_"..player.UserId
leadstats.Parent = player
local success,save = pcall(function()
data = datastore:GetAsync(playerUserId)
end)
if success then
-- Setting saved data to player
checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true)
else
repeat checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true) until success
local webhookdata = {["embeds"] = {{
["title"] = "Error",
["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
local webhookmsg = https:JSONEncode(webhookdata)
task.wait(30)
https:PostAsync(url, webhookmsg)
warn(save)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
--saving
local playerUserId = "Player_"..player.UserId
-- Player int values
local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")
local success, errormsg = pcall(function()
local save
--table
if script:GetAttribute("Loaded" == true) then
local savedata = {}
savedata['chapters'] = chapters.Value
savedata['checkpoints1'] = checkpoints1.Value
print(savedata)
save = data:SetAsync(playerUserId, savedata)
end
end)
if success then
print("Data saved")
else
local webhookdata = {
["embeds"] = {
{
["title"] = "Error",
["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
}
}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url, webhookmsg)
warn(errormsg)
end
end)
game:BindToClose(function()
wait(2)
end)
done but iâve changed my code to @DonKingFrogâs code and i keep getting: ServerScriptService.SavingScript:30: attempt to index nil with âcheckpoints1â
I understand now the problem: You havenât saved anything right? you have to change your code from if success then to if data then:
local datastore2 = require(script.Datastore2)
local Datastore = game:GetService("DataStoreService")
local run = game:GetService("RunService")
local https = game:GetService("HttpService")
local url = (webhook)
local datastore = Datastore:GetDataStore("data")
local data
game.Players.PlayerAdded:Connect(function(player)
--creating values
local leadstats = Instance.new("Folder")
leadstats.Name = "leaderstats"
local checkpoints1 = Instance.new("IntValue", leadstats)
checkpoints1.Name = "Checkpointch1"
local temp = Instance.new("IntValue", leadstats)
temp.Name = "temp"
local chapters = Instance.new("IntValue", leadstats)
chapters.Name = "chapters"
--values
local playerUserId = "Player_"..player.UserId
leadstats.Parent = player
local success,save = pcall(function()
data = datastore:GetAsync(playerUserId)
end)
if data then
-- Setting saved data to player
checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true)
else
repeat checkpoints1.Value = save['checkpoints1'] or 1
chapters.Value = save['chapters'] or 1
print("Data info:", "checkpoints:", checkpoints1.Value ..",")
print("chapters:", chapters.Value ..",")
print("temp", temp.Value)
script:SetAttribute("Loaded", true) until success
local webhookdata = {["embeds"] = {{
["title"] = "Error",
["description"] = "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}}}
local webhookmsg = https:JSONEncode(webhookdata)
task.wait(30)
https:PostAsync(url, webhookmsg)
warn(save)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
--saving
local playerUserId = "Player_"..player.UserId
-- Player int values
local chapters = player:WaitForChild("leaderstats"):WaitForChild("chapters")
local checkpoints1 = player:WaitForChild("leaderstats"):WaitForChild("Checkpointch1")
local temp = player:WaitForChild("leaderstats"):WaitForChild("temp")
local success, errormsg = pcall(function()
local save
--table
if script:GetAttribute("Loaded" == true) then
local savedata = {}
savedata['chapters'] = chapters.Value
savedata['checkpoints1'] = checkpoints1.Value
print(savedata)
save = data:SetAsync(playerUserId, savedata)
end
end)
if success then
print("Data saved")
else
local webhookdata = {
["embeds"] = {
{
["title"] = "Error",
["description"] = script.Name, "Something went wrong while loading data.", player.Name, chapters.Value, checkpoints1.Value}
}
}
local webhookmsg = https:JSONEncode(webhookdata)
https:PostAsync(url, webhookmsg)
warn(errormsg)
end
end)
game:BindToClose(function()
wait(2)
end)