DataStore HELP!

Hello, i am trying to make a datastore which saves the player’s data, if there is no data, it will assign the default data, the default data says ["Kills"] = 5,

Issue: However, I have no data in the game and it doesn’t set my default data to 5 kills. It sets it to 0.

Please know that I am new to DataStore, and it is a real pain.

Please help me, too.

My DataSave script in ServerScriptSerivce:

local dataStoreservice = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local leaderstatsData = dataStoreservice:GetDataStore("data")
local sessionData = {}

function PlayerAdded(player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

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

	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats

	local success = nil
	local playerData = nil
	local attempt = 1
	
		repeat
			success, playerData = pcall(function()
				return leaderstatsData: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 default data")
			playerData = {
				["Kills"] = 5,
				["Deaths"] = 0,
				["Coins"] = 0

			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for", player.Name)
		player:Kick("Unable to load your data. Try again later")
	end
	
	kills.Value = sessionData[player.UserId].Kills
	kills.Changed:Connect(function()
		sessionData[player.UserId].kills = kills.Value
	end)
	leaderstats.Parent = 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()
				leaderstatsData: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
	local playerData = {
		Kills = player.leaderstats.Kills.Value,
		Deaths = player.leaderstats.Deaths.Value,
		Coins = player.leaderstats.Coins.Value
	}
	leaderstatsData:SetAsync(player.UserId, playerData)
end

image
image

2 Likes

Did you enabled access to API services?

Yes i did i think the problem occurs around the print("Connected to database") area

1 Like

Well, i was reading the start and what do you mean dataStoreService?(line 2) You didnt made any local for it or you was should use
game:GetService(“DataStoreService”)
I dont think its the only mistake i will check a little more

Yes i checked it now, i have it on there

I don’t suggest doing it this way, what you could do is something like this:

local kills = Instance.new(“IntValue”)
kills.Name = “Kills”
kills.Value = 5–default value
kills.Parent = leaderstats

—Do all loading script stuff here, if data is nil then it just stays as 5
1 Like

drop me a discord dm: kieranl29 i can just script u a new datastore script if u dont find a fix and post that.

Well probably it says your data loaded sucessfully if its not assigning default so you could try change it in game console to make sure everything works well or not

unless you could change line 3 datastore name so it will be new one and check it does default(new one) or some already existing one which will be the real problem to view

i was going to do that at first, but i followed this tutorial and now ive just wasted my time.

1 Like

I would suggest this if you are new to scritping as I can walk you though it all in depth if you want or just the basics.

im not too new, but im like a intermediate. its just i dont know much about datastore. help me solve this problem plz

On Discord drop me a dm.

i dont understand what that meant

well if you really lack so much i could just give mine one bc im bored to check that stuff so here it is you can change on your own if will need this
leaderstats.rbxm (1.4 KB)

you could change this line like for new data

local leaderstatsData = dataStoreservice:GetDataStore("Somerandomtext")

just post it here man so we can all see whole point of forum

1 Like

Just mark already solution if he helped you

who? i got help but it wasnt the solution to my problem

I suggest you also add a game:BindToClose just in case for your data script

The common issue I see everytime is probably you change everything in the client. If that is then you must change everything in the server.

In order to change from Client To Server look in HOME and look for this button
image
Click and you see everything on the server