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

DataStore script not saving correctly?

I'm making a datastore that saves both money and gears. I started testing the datastore by seeing if it saved money, and it didn't save money. When the players had no save data, though, it set their money value to 520 like the script should do.

Basically my issue is that the script always sets money's value to 520, even if I change the money value to something like 1000 and leave the game. I want it to set the money value to 1000 when I leave the game, not 520. Here's my scipt:

local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("ToolSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game.Players.PlayerAdded:connect(function(Player) --When the player joins the game, load value.
		
	local Stats = Instance.new('Model')
	Stats.Name = "leaderstats"
	
	local Money = Instance.new('NumberValue',Stats)
	Money.Name = "Money"
	
	Stats.Parent = Player

	
	--load data
	local moneyData;
	local toolData;
	
	local CanSave = Instance.new('BoolValue', Player)
	
	CanSave.Name = "CanSaveData"
	CanSave.Value = true
	
	local DataFetchSuccess, ErrorMessage = pcall(function()
		moneyData = ds2:GetAsync(tostring(Player.UserId))
		toolData = ds:GetAsync(tostring(Player.UserId))
	end)
	if DataFetchSuccess then 
		if moneyData ~= nil then --This could be where the problem is
			Money.Value = moneyData
		else
			Money.Value = 520 -- Where money value is changed to 520
		end
		print(Money.Value)
		if toolData ~= nil then
			for i,v in pairs(toolData) do
				print(v)
				local tool = game.ServerStorage.Tools:FindFirstChild(v)
				if toolData then
					toolData:Clone().Parent = Player:WaitForChild("Backpack")
					toolData:Clone().Parent = Player:WaitForChild("StarterGear")
				end			
			end
		end
	else
		Player.CanSaveData.Value = false
		Player:Kick("Your data failed to load! Please rejoin.")
	end
end)

game.Players.PlayerRemoving:connect(function(Player) -- When the player leaves the game, data must save.
	if Player.CanSaveData.Value == false then return end - -[[There could also be a problem with saving the money--]]
	
	local moneyData = Player.leaderstast.Money
	local toolsToSave = {}
	
	for i,v in pairs (Player.Backpack:GetChildren()) do
		if v then
			table.insert(toolsToSave, v.Name)
		end
	end
	
	local DataWriteSuccess, ErrorMessage = pcall(function()
		ds:SetAsync(tostring(Player.UserId), toolsToSave)
		ds2:SetAsync(tostring(Player.UserId), moneyData)
	end)
	
	if not DataWriteSuccess then
		local Retry_Count = 0
		
		while Retry_Count < 6 do
			wait(60)
			local Succeed, Error = pcall(function()
				ds:SetAsync(tostring(Player.UserId), toolsToSave)
				ds2:SetAsync(tostring(Player.UserId), moneyData)
			end)
			if Succeed then break end
			Retry_Count = Retry_Count + 1
		end
	end
end)


Thank you!

1 answer

✓ Accepted answer
local DataStoreService = game:GetService("DataStoreService")
local ds = DataStoreService:GetDataStore("ToolSave")
local ds2 = DataStoreService:GetDataStore("MoneySave")

game:GetService("Players").PlayerAdded:Connect(function(Player) --When the player joins the game, load value.

    local Stats = Instance.new('Folder')
    Stats.Name = "leaderstats"

    local Money = Instance.new('NumberValue', Stats)
    Money.Name = "Money"

    Stats.Parent = Player


    --load data
    local moneyData;
    local toolData;

    local CanSave = Instance.new('BoolValue', Player)

    CanSave.Name = "CanSaveData"
    CanSave.Value = true

    local DataFetchSuccess, ErrorMessage = pcall(function()
        moneyData = ds2:GetAsync(tostring(Player.UserId))
        toolData = ds:GetAsync(tostring(Player.UserId))
    end)
    if DataFetchSuccess then 
        if moneyData ~= nil then --This could be where the problem is
            print("Money save found")
            Money.Value = moneyData
        else
	    print("No money save found.")
            Money.Value = 520 -- Where money value is changed to 520
        end
        print(Money.Value)
        if toolData ~= nil then
            for i,v in pairs(toolData) do
                print(v)
                local tool = game.ServerStorage.Tools:FindFirstChild(v)
                if toolData then
                    toolData:Clone().Parent = Player:WaitForChild("Backpack")
                    toolData:Clone().Parent = Player:WaitForChild("StarterGear")
                end         
            end
        end
    else
        Player.CanSaveData.Value = false
        Player:Kick("Your data failed to load! Please rejoin.")
    end
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player) -- When the player leaves the game, data must save.
    if Player.CanSaveData.Value == false then return end --There could also be a problem with saving the money

    local moneyData = Player.leaderstats.Money.Value
    local toolsToSave = {}

    print(MoneyData)
    print(toolsToSave)

    for i,v in pairs (Player.Backpack:GetChildren()) do
        if v then
            table.insert(toolsToSave, v.Name)
        end
    end

    local DataWriteSuccess, ErrorMessage = pcall(function()
	print("Saving "..Player.Name.."s stats")
        ds:SetAsync(tostring(Player.UserId), toolsToSave)
        ds2:SetAsync(tostring(Player.UserId), moneyData)
    end)

    if not DataWriteSuccess then
        local Retry_Count = 0

        while Retry_Count < 6 do
            wait(60)
            local Succeed, Error = pcall(function()
		print("Attempting to save "..Player.Name.."'s stats.")
                ds:SetAsync(tostring(Player.UserId), toolsToSave)
                ds2:SetAsync(tostring(Player.UserId), moneyData)
            end)
            if Succeed then print("Saved "..Player.Name.."'s stats") break end
            Retry_Count = Retry_Count + 1
        end
    end
end)


9 comments

0

Yeah, I noticed that too and fixed it. It's still not working

0

If I type print(moneyData) in the pcall function and right after it's declared as a variable, it shows nil. If I print it after the function, though, it just prints nothing.

0

I edited the answer, try that

0

Also, you typed "leaderstats" as "leaderstast" on line 55

0

So I'm supposed to load the money instance instead of the value? Or save the value and load the value?

0

edited again

0

Alright, I fixed that (I figured it out after looking at the script for a few minutes) & it's still not working for some reason. I'm using prints to try finding the issue, but I can't find it.

0

I changed the answer to include a few other things. Try that code out and let me know the results.

0

Thanks for your help! It works

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