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

Datastore not working. Not loading data and/or saving?

Sometimes it saves to the storage but when i join it doesnt load. anyone know?

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("LevelStorage")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"	
	
	local cash = Instance.new("NumberValue", leaderstats)
	cash.Name = "Cash"
	cash.Value = 0
	
	local OwnsTycoon = Instance.new("BoolValue", plr)
	OwnsTycoon.Name = "OwnsTycoon"
	OwnsTycoon.Value = false
	
	local level = Instance.new("IntValue",leaderstats)
	level.Name = "Level"
	level.Value = 1
	
	local Leveldata
	
	local success, errormessage = pcall(function()
		Leveldata = dataStore:GetAsync(plr.UserId.."level", level.Value)
	end)
		
	if success then
		cash.Value = Leveldata
		print("Data Loaded from storage")
	else
		print("Error while saving data")
		print(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value)
	end)
	
	if success then
		print("Player Data saved")
	else
		print("Something went wrong")
		warn(errormessage)
	end
end)

1 answer

Usually when I write DataStores I use

Leveldata = dataStore:GetAsync(plr.UserId)

instead of

Leveldata = dataStore:GetAsync(plr.UserId.."level", level.Value)

for GetAsync().

I'm not entirely sure on how this would change much but it's worth a try.

New code:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("LevelStorage")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder", plr)
    leaderstats.Name = "leaderstats"    

    local cash = Instance.new("NumberValue", leaderstats)
    cash.Name = "Cash"
    cash.Value = 0

    local OwnsTycoon = Instance.new("BoolValue", plr)
    OwnsTycoon.Name = "OwnsTycoon"
    OwnsTycoon.Value = false

    local level = Instance.new("IntValue",leaderstats)
    level.Name = "Level"
    level.Value = 1

    local Leveldata

    local success, errormessage = pcall(function()
        Leveldata = dataStore:GetAsync(plr.UserId)
    end)

    if success then
        cash.Value = Leveldata
        print("Data Loaded from storage")
    else
        print("Error while saving data")
        print(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormessage = pcall(function()
        dataStore:SetAsync(plr.UserId.."level",plr.leaderstats.Level.Value)
    end)

    if success then
        print("Player Data saved")
    else
        print("Something went wrong")
        warn(errormessage)
    end
end)

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