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

Datastore not loading values when exiting / rejoining?

still new to coding, help me on ways this can be fixed thank you!!!!

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

game.Players.PlayerAdded:Connect(function(player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "Leaderstats"
	Leaderstats.Parent = player
	
	local Shards = Instance.new("IntValue")
	Shards.Name = "Shards"
	Shards.Parent = Leaderstats
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = Leaderstats
	
	local Face = Instance.new("IntValue")
	Face.Name = "Face"
	Face.Parent = Leaderstats
	
	local Shirts = Instance.new("IntValue")
	Shirts.Name = "Shirts"
	Shirts.Parent = Leaderstats
	
	local Pants = Instance.new("IntValue")
	Pants.Name = "Pants"
	Pants.Parent = Leaderstats
	
	

	
	-- End of Leaderstats setup 
	-- Loading the data...
	
	
	
	
	local playerUserId = "Player_"..player.UserId
	
	local Data
	local success, errorMessage = pcall(function()
		Data = DataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if Data then
			Shards.Value = Data.Shards
			Level.Value = Data.Level
			Face.Value = Data.Face
			Shirts.Value = Data.Shirts
			Pants.Value = Data.Pants
		end
	end
end)




game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local Data = {
		Shards = player.Leaderstats.Shards.Value;
		Level = player.Leaderstats.Level.Value;
		Face = player.Leaderstats.Face.Value;
		Shirts = player.Leaderstats.Shirts.Value;
		Pants = player.Leaderstats.Pants.Value;
	}
	
	local success, errorMessage = pcall(function()
		wait(0.1)
		DataStore:SetAsync(playerUserId, Data)
	end)
	
	if success then
		print("Data has been successfully saved")
	else
		print("Data has failed to save, Error")
		warn(errorMessage)
	end
	
end)

2 answers

✓ Accepted answer

I tested your script and it's working. I'm assuming you tested your script on studio?

This is because studio server shutdowns as you leave/stop. Try using game:BindToClose() for your saving method.

local function save(player)
	local playerUserId = "Player_"..player.UserId

	local Data = {
		Shards = player.Leaderstats.Shards.Value;
		Level = player.Leaderstats.Level.Value;
		Face = player.Leaderstats.Face.Value;
		Shirts = player.Leaderstats.Shirts.Value;
		Pants = player.Leaderstats.Pants.Value;
	}

	local success, errorMessage = pcall(function()
		wait(0.1)
		DataStore:SetAsync(playerUserId, Data)
	end)

	if success then
		print("Data has been successfully saved")
	else
		print("Data has failed to save, Error")
		warn(errorMessage)
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
	save(plr)
end)

game:BindToClose(function() -- runs when game closes
	for i, plr in pairs(game.Players:GetPlayers()) do
		save(plr)
	end
end)

I think the problem is that you typed leaderstats with an uppercase L

Try changing your code from "Leaderstats" to "leaderstats"

1 comment

0

It's not exactly the problem. Typing "Leaderstats" just make them won't show in player list

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