Datastore not working in my game

Can anyone tell me why datastore is not working? I have literally tried 3 different scripts none of them works I even paid scripter to make for me a datastore but doesn’t work either anyone know why? I also have double check the game permissions for API Service its ON

this script is made by the guy i hired

local databaseService  = game:GetService("DataStoreService")
local database = databaseService:GetDataStore("Leaderstats")

game.Players.PlayerAdded:Connect(function(player) 
	local leaderstats = player:WaitForChild("leaderstats")
	local Leaderstats = player:WaitForChild("Leaderstats")
	local rank = leaderstats:WaitForChild("Rank")
	local level = Leaderstats:WaitForChild("Level")
	local xp = Leaderstats:WaitForChild("XP")
	
	
	pcall(function() 
		level.Value = database:GetAsync(player.UserId.."-leaderstatsLevel1") or 1
		rank.Value = database:GetAsync(player.UserId.."-leaderstatsRank1") or "Private"
		xp.Value = database:GetAsync(player.UserId.."-leaderstatsXp1") or 0
	end)
	
end)


while wait(120) do
	for _,player in pairs(game.Players:GetPlayers()) do
		pcall(function() 
			database:SetAsync(player.UserId.."-leaderstatsRank1", player.leaderstats.Rank.Value) 
			database:SetAsync(player.UserId.."-leaderstatsLevel1", player.Leaderstats.Level.Value) 
			database:SetAsync(player.UserId.."-leaderstatsXp1", player.Leaderstats.XP.Value) 
		end)
	end
end


game.Players.PlayerRemoving:Connect(function(player) 
	
	pcall(function() 
		database:SetAsync(player.UserId.."-leaderstatsRank1", player.leaderstats.Rank.Value) 
		database:SetAsync(player.UserId.."-leaderstatsLevel1", player.Leaderstats.Level.Value) 
		database:SetAsync(player.UserId.."-leaderstatsXp1", player.Leaderstats.XP.Value) 
	end)
	
end)

and this one is made by me

local Webhook = "w"
local Https = game:GetService("HttpService")

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("LevelData")

game.Players.PlayerAdded:Connect(function(player)
	
	
	local data
	local success,errormsg = pcall(function()
		data = DataStore:GetAsync('Player_'..player.UserId)
	end)

	local Level = player:WaitForChild("Leaderstats").Level
	local XP = player:WaitForChild("Leaderstats").XP
	local Rank = player:WaitForChild("leaderstats").Rank
	
	if success then
		if data then
			Level.Value = data
			XP.Value = data
			Rank.Value = data
			print(Rank.Value)
			print(Level.Value)
			print(XP.Value)
		else
			warn("Data Does not exist", data)
		end
	else
		warn("Data could not be retrieved!", data)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	 local success,errormessage = pcall(function()
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("Leaderstats").Level.Value)
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("Leaderstats").XP.Value)
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("leaderstats").Rank.Value)
	end)
	
	local failed = {

		content = "Failed to save data, Player_"..player.UserId,
	}
	
	
	local work = {
		
		
		content = "Successfully saved, Player_"..player.UserId,
	}
	
	if not success then
		Https:PostAsync(Webhook,Https:JSONEncode(failed))
		warn("Data could not be saved! Error:",errormessage)
		return
	elseif success then
		Https:PostAsync(Webhook,Https:JSONEncode(work))
	end
	
end)

the script i made only save Rank.Value but not Level and XP

Dont use webhooks to say that the data has succeded or failed as if the webhook is throttled it could get your discord account banned.

And instead of using multiple datastores to store the data just use 1 and use tables instead.

Oh and I think you put 4 capital l’s in leaderstats

This should fix that issue in your script:

local Webhook = "w"
local Https = game:GetService("HttpService")

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("LevelData")

game.Players.PlayerAdded:Connect(function(player)
	
	
	local data
	local success,errormsg = pcall(function()
		data = DataStore:GetAsync('Player_'..player.UserId)
	end)

	local Level = player:WaitForChild("leaderstats").Level
	local XP = player:WaitForChild("leaderstats").XP
	local Rank = player:WaitForChild("leaderstats").Rank
	
	if success then
		if data then
			Level.Value = data
			XP.Value = data
			Rank.Value = data
			print(Rank.Value)
			print(Level.Value)
			print(XP.Value)
		else
			warn("Data Does not exist", data)
		end
	else
		warn("Data could not be retrieved!", data)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	 local success,errormessage = pcall(function()
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("leaderstats").Level.Value)
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("leaderstats").XP.Value)
		DataStore:SetAsync('Player_'..player.UserId,player:WaitForChild("leaderstats").Rank.Value)
	end)
	
	local failed = {

		content = "Failed to save data, Player_"..player.UserId,
	}
	
	
	local work = {
		
		
		content = "Successfully saved, Player_"..player.UserId,
	}
	
	if not success then
		Https:PostAsync(Webhook,Https:JSONEncode(failed))
		warn("Data could not be saved! Error:",errormessage)
		return
	elseif success then
		Https:PostAsync(Webhook,Https:JSONEncode(work))
	end
	
end)

How can I put them all into 1 table? I am not experienced in using tables

Basically just do

local saveTable = {
Level,
XP,
Rank
}

However for that Level,xp etc to save you need a variable of that which i see you already have so ill just write up a version that does that for you

No that is wrong because Level and XP are saved in a seperated folder because I didn’t want them to appear in the UI

1 Like

Ohhhhhh I see in that case let me look

1 Like

Do you mind if you send the place file so i can look a bit deeper?

No sorry I can’t send whole game

Can you send a place file that has the datastore and the leaderstats code in there.
so i can check that instead

There is a 3rd script I have it had same issue do you want to check it too?

Alright I will but give me a second

Sure put that in aswell might be the answer

1 Like

data.rbxl (25.4 KB)

There

Kills/Deaths have rank instance

Ok give me a couple minutes to debug

1 Like

One question whats the actual issue your having cause im seeing alot of things that dont make sense in the code

Which script you mean?

The script I made it only save and load one value which is Rank

Im talking about this script,


Everything in it looks so off

1 Like

This script works perfectly…

I know it just looks really unconventional. anyway

1 Like