I am trying to create a function which saves a player's stage number so they can be teleported there. (I will not include that has not been created yet) Nothing appears in the output when the scripts are ran and I leave. I have published the game in hope of it working there but, it does not. I have tested it twice in the published version of my this. I have created a way to change the Stage value, where the value changes correctly but does not save when I rejoin the game.

local DataService = game:GetService("DataStoreService"):GetDataStore("Stats")



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
	
	local world = Instance.new("IntValue")
	world.Name = "World"
	world.Value = 1
	world.Parent = leaderstats

	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = 0
	deaths.Parent = leaderstats
	
	local key = "player-"..player.userid
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save Format: {world, stages, deaths}
		world.Value = savedValues[1]
		stage.Value = savedValues[2]
	else
		local ValuesToSave = {world.Value, stage.Value}
		DataStore:SetAsync(key, ValuesToSave)
	end
		
	
end)

That is the script that runs when the player joins.

local DataService = game:GetService("DataStoreService"):GetDataStore("Stats")

game.Players.PlayerRemoving:Connect(function(player)
	
	local key = "player-"..player.userid
	
	
	local leaderstats = player.leaderstats
	local ValuesToSave = {leaderstats.world.Value, leaderstats.world.Value}
	--Save Format: {world, stages}
	DataStore:SetAsync(key, ValuesToSave)
	
end)

This is the script that runs when a player leaves the game. All the stats do appear meaning that this script does run. By the way, ignore the deaths leaderboard stat, it is a temporary stat showing how much you have died in that session. I used PeasFactory's Tutorial for this

13 comments

0

Same, it dosen't work for mine too, maybe DataStoreService is down?

0

I hope that is the case

0

I know, my datastore script dosen't work, and everything's spelled correctly. So my best guess is that DataStoreService is down

0

Well, if I can help you, is API services on maybe?

0

What is that? It is probably on default settings since I don't know what it is.

0

API services allows studio to access services like DataStoreService.

0

To turn it on, go to "Game Settings" on studio, then go to Options, and turn API Services on. Maybe that's why?

0

How do I turn that on

0

Above!

0

I couldn't find it in Studio so I looked around onlne for it and found it in the Game Settings, Imma try it now

0

Okay!

0

Sorry, tested it multiple times, it does not function. I guess the API Services are down...

0

Same, it is down.