Help with my Datastore!

Its not saving…

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local database = DatastoreService:GetDataStore(script.CurrentDataStore.Value)
local sessionData = {}

local defaultData = {
	--leaderstats
	["Wins"] = 0,
	
	--towers
	["Anime"] = true,
	["Superhero"] = false,
}

function PlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	
	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
		if not playerData then
			playerData = defaultData
		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
	
	createdata(player)
end

Players.PlayerAdded:Connect(PlayerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

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

			if not success then
				warn(errorMsg)
				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

Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end
	print("Server Shutting Down!")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)

function createdata(player)
	--create values
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats

	local playerdata = Instance.new("Folder")
	playerdata.Name = "PlayerData"
	playerdata.Parent = player

	local towerfolder = Instance.new("Folder")
	towerfolder.Name = "Tower"
	towerfolder.Parent = playerdata

	local itemfolder = Instance.new("Folder")
	itemfolder.Name = "Item"
	itemfolder.Parent = playerdata

	local robuxfolder = Instance.new("Folder")
	robuxfolder.Name = "Robux"
	robuxfolder.Parent = playerdata

	local animevalue = Instance.new("BoolValue")
	animevalue.Name = "anime"
	animevalue.Parent = towerfolder

	local superherovalue = Instance.new("BoolValue")
	superherovalue.Name = "superhero"
	superherovalue.Parent = towerfolder
	
	--set values
	
	wins.Value = sessionData[player.UserId].Wins
	wins.Changed:Connect(function()
		sessionData[player.UserId].Wins = wins.Value
	end)
	
	animevalue.Value = sessionData[player.UserId].Anime
	animevalue.Changed:Connect(function()
		sessionData[player.UserId].Anime = animevalue.Value
	end)
	
	superherovalue.Value = sessionData[player.UserId].Superhero
	superherovalue.Changed:Connect(function()
		sessionData[player.UserId].Superhero = superherovalue.Value
	end)
end

0 ERRORS

1 Like

Is it sending any errors in the output?

1 Like

Sadly not which is making me annoyed!

Alright so, I tested the script in my own game and it worked, fine, I joined with 0 wins, changed it to 5 and then left the game, I rejoined the game and had my 5 wins I left with.

If you are in studio testing, ensure you either A) Enable Studio Access to API Services or B) Test in the actual game.

1 Like

Forgot to add, if you do test in the actual game you can change your Wins Value by using this script in the Command Line in the F9 Dev Console

game.Players.abcxomisgood.leaderstats.Wins.Value = 5

Be sure to change abcxomisgood to your exact ROBLOX username!

2 Likes

It the superhero thing isnt saving… I think its applying default data everytime.

im very aware, thank you! (30 more characters, lol)

Add print statements to see what parts of your code are firing

1 Like

I found my issue. I was setting the value from the client.

I really should go to bed, lol

2 Likes

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