Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Data stores don't work even after multiple attempts of making a working one?

Hello all,

This is my first time working with data stores but for some reason every thing I've tried hasn't worked.

I've watched at least 3 tutorials, following closely each time and every time the data store fails to work/load data in.

On Studio I have API Services enabled, so I don't believe that's the problem.

Here is the Script , located in ServerScriptService

local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints")
game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	local cash = Instance.new("IntValue",folder)
	cash.Name = "Panther Points"
	local saveditems = Data:GetAsync(plr.UserId)
	if saveditems then
		cash.Value = saveditems:WaitForChild("Panther Points").value
	else
		cash.Value = 0
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
	Data:SetAsync(plr.UserId,saved)
end)

Any help would be greatly appreciated.

8 comments

0

Oh let me just give you this link to a video

0

I've tried multiple videos, but thanks for the help

0

See this one

0

It didn't work.

0

@killerninja81 Try changing line 10 to, cash.Value = saveditems["Panther Points"] and let me know if it works or not.

0

ok

0

nope, didnt work

1 answer

✓ Accepted answer

Hey, I know a lot of people have this problem where there's nothing wrong with their script, but the server fails to save their data and reload it when they rejoin. This happens when the server shuts down before the last player leaves, thus not running your PlayerRemoving function which is supposed to save your data. This could also happen when you're playing solo in the Studio. To fix this problem, use :BindToClose. :BindToClose will allow all your remaining functions to run before the server shuts down. Do not delete anything from your original script. Simply copy and paste your PlayerRemoving function (take out PlayerRemoving though and use a loop to loop through all the players in the server) into the :BindToClose function at the end.

local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints")
game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = plr
    local cash = Instance.new("IntValue",folder)
    cash.Name = "Panther Points"
    local saveditems = Data:GetAsync(plr.UserId)
    if saveditems then
        cash.Value = saveditems:WaitForChild("Panther Points").Value -- capitalize the v in Value as well
    else
        cash.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
    Data:SetAsync(plr.UserId,saved)
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		 local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
		 Data:SetAsync(plr.UserId,saved)
	end
end)

3 comments

0

Thanks a lot! It works now, just one little problem, it says that in line 10, it attempts to call a nil value

0

Oh, I didn't catch that. saveditems is your data. Just do "cash.Value = saveditems"

0

Wait no, I fixed it, I made it so that it was like 'if saveditems ~= nil' and swapped the positions of the next 3 lines

This discussion is preserved as it was. New answers, comments, and votes are closed.