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

How can I convert my Data Store script to be compatible with FE?

I made this data store script to save the cash value every 20 seconds. However, according to the console, the value attempted to be saved will always result in the value I saved in studio. I want to make my game FE, however this script refuses to work with FE! Please help... It would be greatly appreciated! (Also I wrote print("imBroken!!!") out of pure frustration, if you where wondering why it was there.)


local Storage = game:GetService('DataStoreService'):GetDataStore("SaveCash")
game.Players.PlayerAdded:connect(function(plr2)
	print("imBroken!!")
	
	local Cash = plr2:WaitForChild("Backpack").PlayerCash
	Cash.Value = 0
	
	local SavedCash = Storage:GetAsync(plr2.userId .. "-Cash")
	if SavedCash ~= nil then
		print(SavedCash)
		Cash.Value = SavedCash
	end
	

while true do
	wait(20)
	print("SAVE")
	local id = plr2.userId
	local CashValue = plr2.Backpack.PlayerCash.Value
	game:GetService('DataStoreService'):GetDataStore("SaveCash"):SetAsync(id.."-Cash", CashValue)
	print(Storage:GetAsync(plr2.userId .. "-Cash"))
end
end)


1 answer

Ideally after the wait you should check to see if the player is still in the game; if not, exit the loop: if not plr2.Parent then break end (though this doesn't fix your problem). Normally you save DataStores to local variables for convenience/conciseness:

local Storage = game:GetService('DataStoreService'):GetDataStore("SaveCash")
game.Players.PlayerAdded:connect(function(plr2)
    local Cash = plr2:WaitForChild("Backpack").PlayerCash -- Are you sure you want this in the Backpack rather than as a direct child of the player? Everything in the Backpack is overwritten every time the character dies.
    Cash.Value = 0
    local key = plr2.userId .. "-Cash"
    local SavedCash = Storage:GetAsync(key)
    if SavedCash ~= nil then
        print(SavedCash)
        Cash.Value = SavedCash
    end
    while true do
        wait(20)
        if not plr2.Parent then break end -- player left
        print("SAVE")
        local id = plr2.UserId -- 'userId' is deprecated
        --local CashValue = plr2.Backpack.PlayerCash.Value
        --If "Cash" still refers to the proper value, use that one
        --Otherwise, use either :WaitForChild or :FindFirstChild
        --I will assume that "Cash" will work:
        Storage:SetAsync(key, CashValue) -- Former problems: you didn't use your Storage variable and you used a non-existent 'id' variable. Open up the Script Analysis window and it'd warn you about problems like the 'id' one.
        print(Storage:GetAsync(key))
    end
end)

Now, if the script still doesn't work, it's likely not this script's fault -- if you're modifying Cash locally, the changes aren't being sent to the server, so the DataStore never receives the up-to-date value. You will need to learn to use RemoteEvents to send the player's actions to the server and the server can then change the player's Cash.

Some of the Guides are useful; also see the Remote Events and Functions tutorial

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