Datastore Help Needed

Hello robloxians, i would really appreaciate some help with this data store thing im having a hard time with.

i need to combine 2 scripts for my game but i dont know how, ive been looking for hours for a answer but cant find a single thing.

these are the 2 scripts i want to combine to make 1.

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("YOUR_DATASTORE_NAME")

game.Players.PlayerAdded:Connect(function(player)
 local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"
 leaderstats.Parent = player
 
 local YOUR_CURRENCY_HERE = Instance.new("IntValue")
 YOUR_CURRENCY_HERE.Name = "YOUR_CURRENCY_NAME_HERE"
 YOUR_CURRENCY_HERE.Parent = leaderstats
 
 local data
 local success, errormessage = pcall(function()
  data = dataStore:GetAsync(player.UserId.."-your_currency_name")
 end)
 
 if success then
  YOUR_CURRENCY_HERE.Value = data
 else
  print("There was an error.")
  warn(errormessage)
 end
end)

game.Players.PlayerRemoving:Connect(function(plr)
 
 local success, errormessage = pcall(function()
  dataStore:SetAsync(plr.UserId.."-your_currency_name", plr.leaderstats.YOUR_CURRENCY_NAME_HERE.Value)
 end)
 
 if success then
  print("Successfully saved Player Data!")
 else
  print("There was an error while saving player data!")
  warn(errormessage)
 end
end)

that is the data store script.

local checkpoints = workspace.CheckPoints

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)   
	end)
end)

this is the script to if a player hits a checkpoint, the leaderstat updates the player to the next checkpoint.

i would like to combine these 2 scripts as i said. cant find a answer. was hoping you could help. thank you for reading

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local YOUR_CURRENCY_HERE = Instance.new("IntValue")
	YOUR_CURRENCY_HERE.Name = "YOUR_CURRENCY_NAME_HERE"
	YOUR_CURRENCY_HERE.Parent = leaderstats
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-your_key_name") -- "-your_key_name" can be changed anything eg "Data"
	end)

	if success then
		YOUR_CURRENCY_HERE.Value = data.Currency
		stage.Value = data.Stage
	else
		print("There was an error.")
		warn(errormessage)
	end
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)   
	end)
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local SaveData={Currency=plr.leaderstats.YOUR_CURRENCY_NAME_HERE.Value, Stage=plr.leaderstats.Stage.Value}
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-your_key_name", SaveData)
	end)

	if success then
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end
	
end)

I’ve changed the datastore slightly to include the Stage variable when saving/loading your player data. If this wasn’t needed then simply change the lines back to how you posted them.

what is a key name? im new to scripting.

Sadly it does not work, ill send you the code that i used from your script and i dont spawn at the spawnpoint when i run the game.

game.Players.PlayerAdded:Connect(function(player)
	local dataStoreService = game:GetService("DataStoreService")
	local dataStore = dataStoreService:GetDataStore("YOUR_DATASTORE_NAME")
	local checkpoints = workspace.CheckPoints

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

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

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats

	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-your_key_name") -- "-your_key_name" can be changed anything eg "Data"
	end)

	if success then
		YOUR_CURRENCY_HERE.Value = data.Currency
		stage.Value = data.Stage
	else
		print("There was an error.")
		warn(errormessage)
	end

	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)   
	end)

end)

game.Players.PlayerRemoving:Connect(function(plr)
	local dataStoreService = game:GetService("DataStoreService")
	local dataStore = dataStoreService:GetDataStore("YOUR_DATASTORE_NAME")
	local SaveData={Currency=plr.leaderstats.YOUR_CURRENCY_NAME_HERE.Value, Stage=plr.leaderstats.Stage.Value}
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-your_key_name", SaveData)
	end)

	if success then
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end

end)

Key Name is just an identifier for your datastore, every players key has to be unique which is why it’s usual to include their UserId in it’s name e.g. plr.UserId…"_PlayerData" will create a key named 000000_PlayerData (exept the 0’s will be the actual userId of the player!)

When you run the script are you getting any output in the output window? Since you have print statements in your code there should be messages saying either Successfully saved Player Data! or There was an error while saving player data!
If your output window isn’t showing then in Studio click the View Tab at the top of the screen and make sure Output is selected underneath.

wait nevermind! it works:D thank you so much:D

1 Like