DataSave is not working

Hello, i tried to find a solution to this bug but i cant find what is the problem with my script. i even tried to copy other saving data scripts but it doesn’t work. The problem is basicly that the data is not saving or its doing it some times but i dont know why. it like saved the data 2-3 times then stopped working i hope i can find somone to help. thats my script.
`local serverstorage = game:GetService(“ServerStorage”)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“PlayerSave1”)

game.Players.PlayerAdded:Connect(function(Player)

local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = Player

local rc = Instance.new("IntValue")
rc.Name = "RC"
rc.Parent = stats

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = stats

local SkillP = Instance.new("IntValue")
SkillP.Name = "SkillPoints"
SkillP.Parent = stats

local WeopenName = Instance.new("StringValue")
WeopenName.Name = "WeopenType"
WeopenName.Parent = stats

--------------- SprintValues ------------------------------------
local Staminafolder = Instance.new("Folder")
Staminafolder.Name = "SprintValues"
Staminafolder.Parent = stats

local maxStamina = Instance.new("IntValue", Staminafolder)
maxStamina.Value = 300
maxStamina.Name = "maxStamina"

local stamina = Instance.new("IntValue", Staminafolder)
stamina.Value = maxStamina.Value
stamina.Name = "Stamina"

local staminaRegen = Instance.new("IntValue", Staminafolder)
staminaRegen.Value = 1
staminaRegen.Name = "staminaRegen"

local sprintModifier = Instance.new("NumberValue", Staminafolder)
sprintModifier.Value = 1.5
sprintModifier.Name = "sprintModifier"

local sprintStaminaCost = Instance.new("IntValue", Staminafolder)
sprintStaminaCost.Value = 1
sprintStaminaCost.Name = "sprintStaminaCost"
-------------------------------------------------------------------------------------------------------------------------

local DataFolder = Instance.new("Folder")
DataFolder.Name = Player.Name
DataFolder.Parent = serverstorage.RemoteData

-- data save

local RCdata, Cashdata, SkillPdata, WeopenNameData, maxStaminaData, sprintModifierData

local success, errormessage = pcall(function()
	RCdata = DataStore:GetAsync("RC-"..Player.UserId)
	Cashdata = DataStore:GetAsync("Cash-"..Player.UserId)
	SkillPdata = DataStore:GetAsync("SkillPoints-"..Player.UserId)
	WeopenNameData = DataStore:GetAsync("WeopenType-"..Player.UserId)
	maxStaminaData = DataStore:GetAsync("MaxStamina-"..Player.UserId)
	sprintModifierData = DataStore:GetAsync("sprintModifier-"..Player.UserId)
end)

if success then
	rc.Value = RCdata
	cash.Value = Cashdata
	SkillP.Value = SkillPdata
	WeopenName.Value = WeopenNameData
	maxStamina.Value = maxStaminaData
	sprintModifier.Value = sprintModifierData	
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStore:SetAsync(“RC-”…player.UserId,player.Stats.RC.Value)
DataStore:SetAsync(“Cash-”…player.UserId,player.Stats.Cash.Value)
DataStore:SetAsync(“SkillPoints-”…player.UserId,player.Stats.SkillPoints.Value)
DataStore:SetAsync(“WeopenType-”…player.UserId,player.Stats.WeopenType.Value)
DataStore:SetAsync(“MaxStamina-”…player.UserId,player.Stats.SprintValues.maxStamina.Value)
DataStore:SetAsync(“sprintModifier-”…player.UserId,player.Stats.SprintValues.sprintModifier.Value)
end)
end)`

1 Like

I switched from datastore to datastore2 a couple of months back.
It’s easier to use, understand and is also more redundant since it uses caching.

2 Likes

Thx so much, i didn’t knew about that

1 Like

Look at all of your amounts of SetAsync(), just store a dictionary instead of 7 keys. That is not all, the game is closing when the event is fired, and the game doesn’t give enough time for the PlayerRemoving event to finish, and the game shuts down too quickly. You would have to set up a function that runs when the game closes using game:BindToClose()

local saveData(plr)
	... -- save a dictionary
end

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for i, plr in ipairs(game.Players:GetPlayers() do
		saveData(plr)
	end
end)

More info can be found here

1 Like

mm i see, but is it better to use that way or the datasave2

Depends, I personally don’t use it, as I am more familiar with normal data stores. It is an option though.

so its not one of them who is more wrong then the other right

Yeah, they both work, so it doesn’t really matter which one you use

Edit: forgot to mention, I use UpdateAsync(), a lot of people have been having more data loss with SetAsync() than UpdateAsync(), and I would recommend UpdateAsync() as well, even the roblox developer hub recommends it

thx for info and help, have a good day :slight_smile:

Datastore2 is created by using datastore but it’s already a complete module that uses some nice tricks like caching that newer developer’s wouldn’t be able to implement their first time around using datastore themselves. It also reduces lines of code :wink:

1 Like

oh its like that it is. thx for the help too

1 Like