My datastore is not working

so I should have bindtoclose after playerRemoving?

Yes you should have that after the playerRemoving.

1 Like
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local database = dataStoreService:GetDataStore("data")
local sessionData = {}

players.PlayerAdded:Connect(function(player)
	local skillFolder = Instance.new("Folder")
	skillFolder.Name = "Skills"
	skillFolder.Parent = player
	local skill = Instance.new("StringValue")
	skill.Name = "Skill"
	skill.Parent = skillFolder

	local weaponFolder = Instance.new("Folder")
	weaponFolder.Name = "Weapons"
	weaponFolder.Parent = player
	local weapon = Instance.new("StringValue")
	weapon.Name = "Weapon"
	weapon.Parent = weaponFolder
	weapon.Value = ""

	local itemFolder = Instance.new("Folder")
	itemFolder.Name = "Items"
	itemFolder.Parent = player
	local item = Instance.new("StringValue")
	item.Name = "Item"
	item.Parent = itemFolder
	item.Value = ""

	local skillCollection = Instance.new("Folder")
	skillCollection.Name = "Skill Collection"
	skillCollection.Parent = player

	local fireballCollected = Instance.new("BoolValue")
	fireballCollected.Name = "Fireball Collected Status"
	fireballCollected.Parent = skillCollection
	fireballCollected.Value = false

	local punchCollected = Instance.new("BoolValue")
	punchCollected.Name = "Punch Collected Status"
	punchCollected.Parent = skillCollection
	punchCollected.Value = false

	local earthSpikesCollected = Instance.new("BoolValue")
	earthSpikesCollected.Name = "Earth Spikes Collected Status"
	earthSpikesCollected.Parent = skillCollection
	earthSpikesCollected.Value = false

	local blueFireball = Instance.new("BoolValue")
	blueFireball.Name = "Blue Fireball Collected Status"
	blueFireball.Parent = skillCollection
	blueFireball.Value = false

	local aquaticTornado = Instance.new("BoolValue")
	aquaticTornado.Name = "Aquatic Tornado Collected Status"
	aquaticTornado.Parent = skillCollection
	aquaticTornado.Value = false

	local hollowPurple = Instance.new("BoolValue")
	hollowPurple.Name = "Hollow Purple Collected Status"
	hollowPurple.Parent = skillCollection
	hollowPurple.Value = false

	local fighting = Instance.new("BoolValue")
	fighting.Name = "playerFightingCheck"
	fighting.Parent = player
	fighting.Value = false

	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to database")
		if not playerData then
			print("Assigning deafult data")
			playerData = {
				Skill = skill.Value
			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for: " .. player.UserId)
		player:Kick("Unable to load your data. Try again later!")
	end

	skill:GetPropertyChangedSignal("Value"):Connect(function()
		sessionData[player.UserId].Skill = skill.Value
	end)
end)

players.PlayerRemoving:Connect(function(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMessage = nil
		local attempt = 1

		repeat
			success, errorMessage = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)

			attempt += 1
			if not success then
				warn(errorMessage)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for " .. player.Name)
		else
			warn("Unable to save for " .. player.Name)
		end
	end
end)
game:BindToClose(function()
	for i, player in ipairs(game.Players:GetPlayers()) do
		if sessionData[player.UserId] then
			local success, errorMessage
			local attempt = 0

			repeat
				attempt += 1
				success, errorMessage = pcall(function()
					database:SetAsync(player.UserId, sessionData[player.UserId])
				end)

				if not success then
					warn("Failed to save data: " .. errorMessage)
					task.wait(3)
				end
			until success or attempt >= 5

			if success then
				print("Data saved for " .. player.Name)
			else
				warn("Unable to save data for " .. player.Name)
			end

			-- Clean up session data
			sessionData[player.UserId] = nil
		end
	end
end)

It said data saved buy when I start playing again saved data does not show up

1 Like

Sorry that I have redone your code but I think this should help you.

local dataStoreService = game:GetService("DataStoreService")
local database = dataStoreService:GetDataStore("data")
local sessionData = {}

local function setupPlayerData(player)
    local skillFolder = Instance.new("Folder")
    skillFolder.Name = "Skills"
    skillFolder.Parent = player

    local skill = Instance.new("StringValue")
    skill.Name = "Skill"
    skill.Parent = skillFolder

    local weaponFolder = Instance.new("Folder")
    weaponFolder.Name = "Weapons"
    weaponFolder.Parent = player

    local weapon = Instance.new("StringValue")
    weapon.Name = "Weapon"
    weapon.Parent = weaponFolder
    weapon.Value = ""

    local itemFolder = Instance.new("Folder")
    itemFolder.Name = "Items"
    itemFolder.Parent = player

    local item = Instance.new("StringValue")
    item.Name = "Item"
    item.Parent = itemFolder
    item.Value = ""

    local skillCollection = Instance.new("Folder")
    skillCollection.Name = "Skill Collection"
    skillCollection.Parent = player

    local skills = {
        "Fireball Collected Status",
        "Punch Collected Status",
        "Earth Spikes Collected Status",
        "Blue Fireball Collected Status",
        "Aquatic Tornado Collected Status",
        "Hollow Purple Collected Status"
    }

    for _, skillName in ipairs(skills) do
        local skillValue = Instance.new("BoolValue")
        skillValue.Name = skillName
        skillValue.Parent = skillCollection
        skillValue.Value = false
    end

    local fighting = Instance.new("BoolValue")
    fighting.Name = "playerFightingCheck"
    fighting.Parent = player
    fighting.Value = false

    return skill
end

local function loadPlayerData(player, skill)
    local success, playerData
    local attempt = 1

    repeat
        success, playerData = pcall(function()
            return database:GetAsync(player.UserId)
        end)

        if not success then
            warn(playerData)
            task.wait(3)
        end
        attempt += 1
    until success or attempt == 5

    if success then
        print("Connected to database")
        if not playerData then
            print("Assigning default data")
            playerData = {Skill = skill.Value}
        end
        sessionData[player.UserId] = playerData
    else
        warn("Unable to get data for: " .. player.UserId)
        player:Kick("Unable to load your data. Try again later!")
    end

    skill:GetPropertyChangedSignal("Value"):Connect(function()
        if sessionData[player.UserId] then
            sessionData[player.UserId].Skill = skill.Value
        end
    end)
end

local function savePlayerData(player)
    if sessionData[player.UserId] then
        local success, errorMessage
        local attempt = 1

        repeat
            success, errorMessage = pcall(function()
                database:SetAsync(player.UserId, sessionData[player.UserId])
            end)

            if not success then
                warn(errorMessage)
                task.wait(3)
            end
            attempt += 1
        until success or attempt == 5

        if success then
            print("Data saved for " .. player.Name)
        else
            warn("Unable to save for " .. player.Name)
        end
    end
end

players.PlayerAdded:Connect(function(player)
    local skill = setupPlayerData(player)
    loadPlayerData(player, skill)
end)

players.PlayerRemoving:Connect(function(player)
    savePlayerData(player)
end)

game:BindToClose(function()
    for _, player in ipairs(players:GetPlayers()) do
        savePlayerData(player)
        sessionData[player.UserId] = nil
    end
end)
1 Like

still isn’t working, it says data connected and data saved, but nothing actually happens

I’ll send you a recording actually, I feel like that would help

1 Like

Is there anything else on the output (F9) that comes up besides data connected and data saved?

1 Like

Please try this.

local dataStoreService = game:GetService("DataStoreService")
local database = dataStoreService:GetDataStore("data")
local players = game:GetService("Players")
local sessionData = {}

local function setupPlayerData(player)
    local skillFolder = Instance.new("Folder")
    skillFolder.Name = "Skills"
    skillFolder.Parent = player

    local skill = Instance.new("StringValue")
    skill.Name = "Skill"
    skill.Parent = skillFolder

    local weaponFolder = Instance.new("Folder")
    weaponFolder.Name = "Weapons"
    weaponFolder.Parent = player

    local weapon = Instance.new("StringValue")
    weapon.Name = "Weapon"
    weapon.Parent = weaponFolder
    weapon.Value = ""

    local itemFolder = Instance.new("Folder")
    itemFolder.Name = "Items"
    itemFolder.Parent = player

    local item = Instance.new("StringValue")
    item.Name = "Item"
    item.Parent = itemFolder
    item.Value = ""

    local skillCollection = Instance.new("Folder")
    skillCollection.Name = "SkillCollection"
    skillCollection.Parent = player

    local skills = {
        "FireballCollected",
        "PunchCollected",
        "EarthSpikesCollected",
        "BlueFireballCollected",
        "AquaticTornadoCollected",
        "HollowPurpleCollected"
    }

    for _, skillName in ipairs(skills) do
        local skillValue = Instance.new("BoolValue")
        skillValue.Name = skillName
        skillValue.Parent = skillCollection
        skillValue.Value = false
    end

    local fighting = Instance.new("BoolValue")
    fighting.Name = "IsFighting"
    fighting.Parent = player
    fighting.Value = false

    return skill
end

local function loadPlayerData(player, skill)
    local success, playerData
    local attempt = 1

    repeat
        success, playerData = pcall(function()
            return database:GetAsync(player.UserId)
        end)

        if not success then
            warn(playerData)
            task.wait(3)
        end
        attempt += 1
    until success or attempt == 5

    if success then
        print("Connected to database")
        if not playerData then
            print("Assigning default data")
            playerData = {Skill = skill.Value}
        end
        sessionData[player.UserId] = playerData
    else
        warn("Unable to get data for: " .. player.UserId)
        player:Kick("Unable to load your data. Try again later!")
    end

    skill:GetPropertyChangedSignal("Value"):Connect(function()
        if sessionData[player.UserId] then
            sessionData[player.UserId].Skill = skill.Value
        end
    end)
end

local function savePlayerData(player)
    if sessionData[player.UserId] then
        local success, errorMessage
        local attempt = 1

        repeat
            success, errorMessage = pcall(function()
                database:SetAsync(player.UserId, sessionData[player.UserId])
            end)

            if not success then
                warn(errorMessage)
                task.wait(3)
            end
            attempt += 1
        until success or attempt == 5

        if success then
            print("Data saved for " .. player.Name)
        else
            warn("Unable to save for " .. player.Name)
        end
    end
end

players.PlayerAdded:Connect(function(player)
    local skill = setupPlayerData(player)
    loadPlayerData(player, skill)
end)

players.PlayerRemoving:Connect(function(player)
    savePlayerData(player)
end)

game:BindToClose(function()
    for _, player in ipairs(players:GetPlayers()) do
        savePlayerData(player)
        sessionData[player.UserId] = nil
    end
end)

1 Like

I’ll have to reply to you tomorrow as I’m tired from being on roblox studio all day(literally). I think you got rid of a folder or value in the code and now its causing issues. However, thank you so much for sticking with me on this issue all this time. I really appreciate it!

1 Like

The issue was that I was storing the skill value on the client side on a different script, and that I may have been using numbers instead of strings for the value. Thank you to everyone who has helped me out with this issue, and I’m sorry I didn’t realize the client side issue earlier.

1 Like

If you want to use values but also tables or not use this: FastValue a replacement for Value Instances that is better than Remotes and Bindables

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.