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

Can someone help with my Data Store problem?

So, I made this out of what CodeTheorem gave me. When I say /save, it prints Saved. When I say /load, it prints loaded. But, when I load after saving, my cash gets set to 0 and doesn't continue to increase. After reading through the code, to me it seems like it's saving the names of the Values and not the actual values of them. When I load from the Data Store, I want it to set each Value to the value they were at when I saved.

local KEY_PREFIX = "userid_"
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore")
local profilesHolder = game.Workspace.Profiles
Debounce = true

function saveToDataStore(plr)
	if Debounce == true then
		Debounce = false	
    local key = KEY_PREFIX .. tostring(plr.userId) 
    local profile = profilesHolder[plr.Name]
    local value = {}
    for i,v in ipairs(profile:GetChildren()) do 
        value[v.Name] = v.Value
        print(v.Name .. " = " .. tostring(v.Value))
    end
    ds:SetAsync(key, value)
	wait(15)
	print("Saved")
	Debounce = true
	elseif Debounce == false then
		print("Not ready")
end
end

function loadFromDataStore(plr)
	if Debounce == true then
		Debounce = false	
    local key = KEY_PREFIX .. tostring(plr.userId)
    local data = ds:GetAsync(key)
    local profile = profilesHolder[plr.Name]
    local profileItems = profile:GetChildren()
    for i,v in ipairs(data) do
        profileItems[i].Value = v
        print(i .. " = " .. tostring(v))
	end
	wait(15)
    print("Loaded")
	Debounce = true
	elseif Debounce == false then
		print("Not ready")
    end
end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        msg = msg:lower()
        if msg == "/save" then
            saveToDataStore(player)
        elseif msg == "/load" then
            loadFromDataStore(player)

        end
    end)
end)

And here's the script that creates the values.

game.Players.PlayerAdded:connect(function(player)
    Profile = Instance.new("Model")
    Profile.Parent = script.Parent
    Profile.Name = (player.Name)    
    Cash = Instance.new("NumberValue")
    Cash.Parent = script.Parent[player.Name]
    Cash.Name = "Cash"
    Cash.Value = 0
    a = Cash:Clone()
    a.Name = "WaitTime"
    a.Parent = script.Parent[player.Name]
    a.Value = 5
    a = Cash:Clone()
    a.Name = "Block1"
    a.Parent = script.Parent[player.Name]
    a = Cash:Clone()
    a.Name = "Block2"
    a.Parent = script.Parent[player.Name]
end)

2 answers

I'm sorry - I was really tired when I tried to help you last night and I had one small error in my code. I do sincerely apologize. The below code will work.

local KEY_PREFIX = "userid_"
local ds = game:GetService("DataStoreService"):GetDataStore("PlayerValuesDataStore")
local profilesHolder = game.Workspace.Profiles 

function saveToDataStore(plr)
	local key = KEY_PREFIX .. tostring(plr.userId) 
	local profile = profilesHolder[plr.Name]
	local value = {}
	for i,v in ipairs(profile:GetChildren()) do 
		value[v.Name] = v.Value
		print(v.Name .. " = " .. tostring(v.Value))
	end
	ds:SetAsync(key, value)
end

function loadFromDataStore(plr)
	local key = KEY_PREFIX .. tostring(plr.userId)
	local data = ds:GetAsync(key)
	local profile = profilesHolder[plr.Name]
	local profileItems = profile:GetChildren()
	for i,v in ipairs(data) do
		profileItems[i].Value = v
		print(i .. " = " .. tostring(v))
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		msg = msg:lower()
		if msg == "/save" then
			saveToDataStore(player)
			print("Saved")
		elseif msg == "/load" then
			loadFromDataStore(player)
			print("Loaded")
		end
	end)
end)

14 comments

0

When I save, it prints Saved. When I load it prints Loaded. It doesn't break the money gathering system anymore, but it doesn't load/save the data either. Is it a problem I created with my Chatted script? I could possibly not be gathering player correctly.

0

I've updated my script so that it will print the names of each item in the profile and their values on save and load. Mind replacing the script again and telling me what that looks like?

0

The problem is in the Loading. It saves and prints all the values, but when I load it prints Loaded but nothing else

0

Are you waiting at least 15 seconds in between the "/save" and "/load" commands? ROBLOX's DataStore caches every ten seconds, so you need to.

0

I will make it wait before trying, and then try again

0

It's still not working. I added the wait. And updated script can be found in the question. It doesn't print or load a list of values after loading. When I save it lists all the values however.

0

Are there any errors (red text)?

0

None at all

0

Well, we do have 15:40:38.156 - httpGet https://api.roblox.com/gametransactions/getpendingtransactions/?PlaceId=10965599&PlayerId=54680 failed. Trying again. Error: Forbidden. Elapsed time: 0.193088 15:40:38.157 - httpGet failed. Trying again. Elapsed time: 0.193088

0

No Red however

0

That httpGet has nothing to do with this. Okay...Can you give me a link to the game that this is at?

0

It's only printing to server console, not really sure how to get it to print to the local console.

0

I sent you a link on Twitter for a download of the place

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