no I added a waitforchild for the axes folder.
oh, well it still did not work.
It SHOULD look like the following:
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("datatest")
game.Players.PlayerAdded:Connect(function(plr)
local axes = Instance.new("Folder",plr)
axes.Name = "axes"
local Homemadeaxe = Instance.new("BoolValue",axes)
Homemadeaxe.Name = "Homemadeaxe"
local Normalaxe = Instance.new("BoolValue",axes)
Normalaxe.Name = "Normalaxe"
local dataofuser = ds:GetAsync(plr.UserId)
if dataofuser ~= nil then -- anything but nil
print("Found data for " .. plr.Name)
Homemadeaxe.Value = ds[1]
Normalaxe.Value = ds[2]
else
print("Replacing no data with new data.")
Homemadeaxe = true
Normalaxe = false
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local datasave = {}
table.insert(datasave, plr:WaitForChild("axes"):WaitForChild("Homemadeaxe"):WaitForChild("BoolValue").Value)
table.insert(datasave, plr:WaitForChild("axes"):WaitForChild("Normalaxe"):WaitForChild("BoolValue").Value)
local success,response = pcall(function()
ds:SetAsync(plr.UserId, datasave)
end)
if success then
print("succesfully saved data of " .. plr.Name)
else
warn(response)
end
end)
sadly, it did not work, do you have any other ideas?
Like @PostedDevOfficial said, you’re not looking in the right place. Don’t use WaitForChild
there either. I would also like to point out both of you were attempting to find an instance that does not exist, the BoolValue
under each axe, because it was never created. This causes WaitForChild
to yield infinitely.
local function save(player:Player)
local saveData = {
[1] = plr.axes.Homemadeaxe.Value,
[2] = plr.axes.Normalaxe.Value
}
local attempt = 0
local success
local result
repeat
success, result = pcall(dataStore.UpdateAsync, dataStore, plr.UserId, function(old) return saveData end)
attempt += 1
until
success or attempt == 3
if not success then
warn(result)
end
end
game:GetService("Players").PlayerRemoving:Connect(save)
I think you also need a BindToClose
subprogram. This can ensure the game has enough time to save all player’s data before it closes. You can add this at the bottom:
local runService = game:GetService("RunService")
local players = game:GetService("Players")
game:BindToClose(function()
if runService:IsStudio() or #players:GetPlayers() <= 1 then task.wait(3) return nil end
for _, player in next, players:GetPlayers(), nil do
save(player)
end
task.wait(3)
end)
You should also use a pcall
for GetAsync
.
where would i put this in my script?
Replace your game.Players.PlayerRemoving
with the top script (include the connection) and add the other script at the bottom. Put the services at the top, though.
on line 4 of the second script it is saying Untitled Game @ 07 May 2024 17:07 auto-recovery file was created - Studio
17:07:11.070 ServerScriptService.Axes:48: Expected ‘(’, ‘{’ or when parsing function call, got ‘<=’
My bad, I kind of forgot the parenthesis after GetPlayers
… I fixed it in the script. Please also make sure to wrap GetAsync
in a pcall
.
where would i put the getasync
In your PlayerAdded
code.
Just changes to:
local success, dataofuser = pcall(ds.GetAsync, ds, plr.UserId)
if not success then
plr:Kick("Failed to load data. Please rejoin!")
end
It just prevents further issues.
is this right, I am having issues on line 11 and line 18
local function save(player:Player)
local saveData = {
[1] = player.axes.Homemadeaxe.Value,
[2] = player.axes.Normalaxe.Value
}
local attempt = 0
local result
repeat
local success, dataofuser = pcall(ds.GetAsync, ds, player.UserId)
attempt += 1
until
success or attempt == 3
if not success then
player:Kick("Failed to load data. Please rejoin!")
end
end
local runService = game:GetService("RunService")
local players = game:GetService("Players")
game:BindToClose(function()
if runService:IsStudio() or pcall(#players:GetPlayers <= 1) then task.wait(3) return nil end
for _, player in next, players:GetPlayers(), nil do
save(player)
end
task.wait(3)
end)
game:GetService("Players").PlayerRemoving:Connect(save)
No. I told you to put it in your PlayerAdded
code, and quoted which part of your script you should replace.
You also do not need any kind of pcall
in the BindToClose
code, it will error because you aren’t passing a function to it.
Please refer to my previous code I sent for the saving, and replace the GetAsync
code you currently have in the joining code with the code I sent.
GetAsync
is for loading data, UpdateAsync
, SetAsync
and IncrementAsync
are for saving data. In this case, I used UpdateAsync
in my example.
sorry for the misinterperation, it is saying that 1 is not a vlid member of datastore "datasotreservice.datatest?
You seem to have made a slight spelling error. Change UpdateAsynic
to UpdateAsync
.
It’s just a route to data saving that doesn’t require making another function, putting dot notation in the pcall
.
Hang on, you edited your post.
sorry but in my script it is UpdateAsync also can help me with the savedata thing it is sayiong that 1 is not part of datastore srevice.datatest
What line is that error coming from? The part you quoted seems fine.
Please include the surrounding code as well as the line number.
im sorry
local function save(player:Player)
local saveData = {
[1] = player.axes.Homemadeaxe.Value,
[2] = player.axes.Normalaxe.Value
}
Dont be sorry, theres nothing to apologise for.
That code all looks fine as well, are you sure that’s where it’s erroring? Can you send your new save code?
whoops wrong area
if not success then
plr:Kick("Failed to load data. Please rejoin!")
end
if dataofuser ~= nil then -- anything but nil
print("Found data for " .. plr.Name)
Homemadeaxe.Value = ds[1]
Normalaxe.Value = ds[2]
else
print("Replacing no data with new data.")
Homemadeaxe = true
Normalaxe = false