Data Store Problem

hi guys, i have a problem with this data store script. its saves and load my leaderstats correctly but it dont saves and load the levels that the player has unlocked. everytime when a player joins the game he is at level 2

here is the code

local DataStoreService = game:GetService("DataStoreService")
local PlayerCashStore = DataStoreService:GetDataStore("PlayerCashStore") -- You can rename the string in quotation marks to whatever you want
local PlayerGemStore = DataStoreService:GetDataStore("PlayerGemStore") -- You can rename the string in quotation marks to whatever you want
local SaveUnlockedLevel = DataStoreService:GetDataStore("SaveUnlockedLevelData")

function savedata(player) -- Call this function whenever you need to save data
	local success, err = pcall(function()
		PlayerCashStore:SetAsync(player.UserId, player.leaderstats.Cash.Value)
		PlayerGemStore:SetAsync(player.UserId, player.leaderstats.Gems.Value)
		SaveUnlockedLevel:SetAsync(player.UserId, game.Workspace.CurrentLevel.UnlockedLevels.Value)
	end)
	if success then
		print("Yippie, Saved player's data successfully!")
		
	else
		print(err) -- Prints the error that had happened
	end
end

game.Players.PlayerRemoving:Connect(function(player) -- When player leaves
	savedata(player)
end)

game:BindToClose(function() -- When server shuts down
	for i, player in pairs(game.Players:GetPlayers()) do
		savedata(player)
	end
end)


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

	local Cash = Instance.new("IntValue") -- Cash can be renamed to whatever you want
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = PlayerCashStore:GetAsync(player.UserId) or 0
	
	local Gems = Instance.new("IntValue") -- Cash can be renamed to whatever you want
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	Gems.Value = PlayerGemStore:GetAsync(player.UserId) or 0
	
	game.Workspace.CurrentLevel.Level.Value = game.Workspace.CurrentLevel.UnlockedLevels.Value
end)

if you need more scripts or any screenshots let me know.

cheers

It’s probably because you never actually retrieve the value from the datastore.

Change your PlayerAdded block to this:

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

	local Cash = Instance.new("IntValue") -- Cash can be renamed to whatever you want
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = PlayerCashStore:GetAsync(player.UserId) or 0
	
	local Gems = Instance.new("IntValue") -- Cash can be renamed to whatever you want
	Gems.Name = "Gems"
	Gems.Parent = leaderstats
	Gems.Value = PlayerGemStore:GetAsync(player.UserId) or 0
	
        local level = SaveUnlockedLevel:GetAsync(player.UserId)
        game.Workspace.CurrentLevel.UnlockedLevels.Value = level
	game.Workspace.CurrentLevel.Level.Value = level
end)

i would try it with this solution

the loader and all that works fine but my ball is not showing when i join i need to reset me everytime how could i fix that.

Have you got code for spawning the ball when the player joins? I don’t believe anything is wrong with the actual spawning of it I just don’t think you’re calling it to spawn on PlayerAdded.

everytime a player spawns the ball is at level one

Can you show the code which spawns the ball on PlayerAdded?

yea wait just a moment i need to find it

i only have a config module with all positions in each level

local ConfigModule = {}

ConfigModule.ResetPositions = { -- THESE ARE SEQUENCED IN ORDER FOR THE LEVELS
	-- DO NOT FORGET TO INCLUDE THE COMMAS BETWEEN THEM
	Vector3.new(-4.461, 83.55, -26.55), -- LV1
	Vector3.new(-289.561, 83.55, -26.55), -- LV2
	Vector3.new(-549.361, 83.55, -26.55), -- ETC
	Vector3.new(-887.761, 45.45, -38.693),
	Vector3.new(-1135.461, 83.25, -38.693),
	Vector3.new(-1422.861, 83.25, -38.693),
	Vector3.new(-1735.661, 76.75, -41.893),
	Vector3.new(-2088.761, 37.65, -41.893),
	Vector3.new(-2329.661, 76.85, -41.893),
	Vector3.new(-2655.461, 76.85, -41.893)
}

return ConfigModule

Yeah, so you’ll need to call the spawning of it back in your main script. You’ve defined the position of it but you’re not actually spawning it. The code to spawn it should be similar to the reset button code.

The script should access the module and data store, check the level of the player, and then set the position value based on the level from the data store.

can you give me an solution code what to add to my data store script that the ball is loading in the correct level?

I can’t give you actual code as I’m away from my computer right now, but I’ve outlined what you need to do in my last post.

yea but i really dont know how to do that im a scripting beginner

If nobody helps you by the time I’m back, I can help you properly. :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.