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

is there anything wrong in my data save script?

for some reason its not working, no errors

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("Cash24")
game.Players.PlayerAdded:Connect(function(plr)
	local cash = game.ServerStorage.MoneyStorage:WaitForChild(plr.Name)
	local success,err = pcall(function()
		cash.Value = ds1:GetAsync(plr.UserId) or 0
	end)
	if success then
		print("Your Data Has Been Loaded.")
	else
		print("Your Data Loading Has Failed.")
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local cash = game.ServerStorage.MoneyStorage:FindFirstChild(plr.Name)
	local success,err = pcall(function()
		ds1:SetAsync(cash.Value, plr.UserId)
	end)
	if success then
		print("Your Data Has Been Saved.")
	else
		print("We Got An Error While Saving Your Data.")
		warn(err)
	end
end)

2 answers

Nothing seems wrong with your script try this:

Fixed version Make sure you turned on enable studio api in game settings.

local DATA_STORE_NAME = "Config"
local DATA_STORE_SCOPE = "MyGameName"
local KEY_NEW_FEATURE = "NewFeatureEnabled"

local DataStoreService = game:GetService("DataStoreService")

local dsConfig = DataStoreService:GetDataStore(DATA_STORE_NAME, DATA_STORE_SCOPE)

local function checkFeature()
    local isFeatureEnabled
    local success, err = pcall(function()
        isFeatureEnabled = dsConfig:GetAsync(KEY_NEW_FEATURE)
    end)
    if success then
        print(KEY_NEW_FEATURE .. ": " .. tostring(isFeatureEnabled))
        if isFeatureEnabled == true then
            print("Feature is enabled!")
        elseif isFeatureEnabled == false then
            print("Feature is disabled!")
        elseif isFeatureEnabled == nil then
            print("Feature is not set!")
        else
            -- Some other value was found in this key
        end
    else
        print("Failed to load feature! Error: " .. tostring(err))
    end
    return success
end

local function onFeatureChanged(isFeatureEnabled)
    print("Feature toggled: " .. tostring(isFeatureEnabled))
end

-- Listen for changes
dsConfig:OnUpdate(KEY_NEW_FEATURE, onFeatureChanged)

local function setFeatureEnabled(isFeatureEnabled)
    local success, err = pcall(function()
        dsConfig:SetAsync(KEY_NEW_FEATURE, isFeatureEnabled)
    end) 
    return success
end

setFeatureEnabled(true)
checkFeature()

Found the issue! Your SetAsync parameters were in an incorrect order. For example, if my user id was something like 39485902 and I got 50 money and left, it saves it under the number 50 as the owner. What your SetAsync should look like is this:

DataStore:SetAsync(plr.UserId,cash.Value)

Hope this helped!

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