Datastore is not working

  1. What do you want to achieve? I want the player’s strength and rebirths to save

  2. What is the issue? I’ve enabled api services and it still doesn’t save

  3. What solutions have you tried so far? I’ve looked in a bunch of similar topics but nothing worked, and I’ve checked my script for any grammar mistakes and found none

here’s the code:

local serverStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave1")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	local strength = Instance.new("IntValue")
	strength.Name = "Strength"
	strength.Parent = leaderstats
	
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = player.Name
	dataFolder.Parent = serverStorage.RemoteData
	
	local debounce = Instance.new("BoolValue")
	debounce.Name = "debounce"
	debounce.Parent = dataFolder
	
	local strengthData,rebirthsData
	
	local success,errormessage = pcall(function()
		strengthData = DataStore:GetAsync("strength-"..player.Name)
		rebirthsData = DataStore:GetAsync("rebirths-"..player.Name)
	end)
	
	if success then
		if strengthData then
			strength.Value = strengthData
			rebirths.Value = rebirthsData
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success,errormessage = pcall(function()
		DataStore.SetAsync("strength-"..player.Name,player.leaderstats.Strength.Value)
		DataStore.SetAsync("rebirths-"..player.Name,player.leaderstats.Rebirths.Value)
	end)
end)

This should be tested in game for it to save. Studio closes the game when you stop it. So use game:BindToClose() to fix it so it saves in Studio. Also,

use : not .

1 Like

thank you so much, I’m gonna implement that into the code and if it works I’ll mark it as a solution