I know this is a very general topic but I’m very confused
local dstore = game:GetService("DataStoreService")
local deathnoteTexture = dstore:GetDataStore("deathNoteTextureId")
game.Players.PlayerAdded:Connect(function(plr)
local texture = Instance.new("IntValue")
texture.Name = plr.UserId.."-noteid"
texture.Parent = game.ServerStorage
local textureValue
local success, err = pcall(function()
textureValue = deathnoteTexture:GetAsync(plr.UserId.."-noteid")
end)
if success then
texture.Value = textureValue
else
print("Error")
warn(err)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, err = pcall(function()
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
if success then
print("Data saved")
else
print("error")
warn(err)
end
end)
i have 2 other datastore thinks, both handled in this same script at the same time. they are identical to this one, and those 2 both work, except for some reason, this one doesnt
the ONLY difference i can find is that the other 2 saved values were inside a leaderstats folder, but idk if that means anything
This is probably because you’re not using game:BindToClose() so the server closes before the PlayerRemoving event can fire, to solve this just add a function with game:BindToClose():
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
end
end)
i had figured id have to use this
well, no better time to learn now
now, some questions-
do i replace the entire player removing with bind to close? or just do this for this new one?
do i have to change the playeradded function as well? (i assume not but just checking)
No, you don’t have to do anything with those events, :BindToClose() fires when the server is closes, so it’s just to add security and prevent this type of errors, so you just have to add it to the script.
alright, one final question, just for future knowledge-
i can tell what everything else in the script does, but what exactly does the bindtoclose() function accomplish?
It’s like another roblox event, but you don’t have to use :Connect() to connect it to a function and this fires when the server is closed (has the priority of firing before the server closes thing that the PlayerRemoving event can’t handle).
also, heres the new script which will be relevant to my question
game.Players.PlayerRemoving:Connect(function(plr)
local success, err = pcall(function()
killsStore:SetAsync(plr.UserId.."-kills", plr.leaderstats.Kills.Value)
repStore:SetAsync(plr.UserId.."-rep", plr.leaderstats["Kira Rep"].Value)
end)
if success then
print("Data saved")
else
print("error")
warn(err)
end
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
end
end)
so youre seeing some new datastore things that i removed before for convenience, such as killstore and repstore
these work and save correctly in the playerremoving event…
should i just keep this script the way it is, or add these two datastores to the bindtoclose as well, just to be safe?
should i just delete the player removing and only use the bindtoclose?
You don’t have to delete the PlayerRemoving event, because :BindToClose() just fires when all players have left the game, so you will need to have both; BindToClose and PlayerRemoving. And yes it’s better to have this function on all datastores, even if they already work.
game.Players.PlayerRemoving:Connect(function(plr)
local success, err = pcall(function()
killsStore:SetAsync(plr.UserId.."-kills", plr.leaderstats.Kills.Value)
repStore:SetAsync(plr.UserId.."-rep", plr.leaderstats["Kira Rep"].Value)
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
if success then
print("Data saved")
else
print("error")
warn(err)
end
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
killsStore:SetAsync(plr.UserId.."-kills", plr.leaderstats.Kills.Value)
repStore:SetAsync(plr.UserId.."-rep", plr.leaderstats["Kira Rep"].Value)
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
end
end)
I mean… you obviously also need PlayerAdded event, I will better write this code so you can understand:
game.Players.PlayerAdded:Connect(function(plr)
local texture = Instance.new("IntValue")
texture.Name = plr.UserId.."-noteid"
texture.Parent = game.ServerStorage
local textureValue
local success, err = pcall(function()
textureValue = deathnoteTexture:GetAsync(plr.UserId.."-noteid")
end)
if success then
texture.Value = textureValue
else
print("Error")
warn(err)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, err = pcall(function()
killsStore:SetAsync(plr.UserId.."-kills", plr.leaderstats.Kills.Value)
repStore:SetAsync(plr.UserId.."-rep", plr.leaderstats["Kira Rep"].Value)
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
if success then
print("Data saved")
else
print("error")
warn(err)
end
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
killsStore:SetAsync(plr.UserId.."-kills", plr.leaderstats.Kills.Value)
repStore:SetAsync(plr.UserId.."-rep", plr.leaderstats["Kira Rep"].Value)
deathnoteTexture:SetAsync(plr.UserId.."-noteid", game.ServerStorage[plr.UserId.."-noteid"].Value)
end)
end
end)
does this have to be tested in a real game in order to work? because im only in studio
like i have the api thing enabled and the other two datastores are working fine