Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Datastore won't save! Saving won't work! Please help?

Problem: I made a Datastore where every time you punch it increases your experience[IntValue] by 1. So I tested it I changed my experience number to 7 and left the game. I came back to check and this time it was 0. I left the server again and came back it was 7. I did this a couple times. Sometimes its 7 and sometimes it is 0. I have no clue whats happening!

Here is the Datastore(Workspace)

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

game.Players.PlayerAdded:connect(function(player)
	local Key = "Player-"..player.userId
	local playerFolder = Instance.new("Folder", player)
	playerFolder.Name = "PlayerFolder"
	local statFolder = Instance.new("Folder", playerFolder)
	statFolder.Name = "Stats"
	local strength = Instance.new("IntValue", statFolder)
	strength.Name = "Strength"
	strength.Value = 0
	local defense = Instance.new("IntValue",statFolder)
	defense.Name = "Defense"
	defense.Value = 0
	local sword = Instance.new("IntValue", statFolder)
	sword.Name = "Sword"
	sword.Value = 0
	local hp = Instance.new("IntValue", statFolder)
	hp.Name = "Health"
	hp.Value = 100
	local SLevel = Instance.new("IntValue", statFolder)
	SLevel.Name = "SLevel"
	SLevel.Value = 0
	
	strength.Changed:connect(function()
		SLevel.Value = math.floor(strength.Value/200)
	end)
	
	local Data1 = Data:GetAsync(Key)
	
	if Data1 then
		strength.Value = Data1[1]
		defense.Value = Data1[2]
		sword.Value = Data1[3]
		hp.Value = Data1[4]
		SLevel.Value = Data1[5]
	else
		local ThingsToSave = {strength.Value,defense.Value,sword.Value,hp.Value,SLevel.Value}
		Data:SetAsync(Key,ThingsToSave)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local Key = "Player-"..player.userId
	local Save = {player.PlayerFolder.Stats.Sword.Value,player.PlayerFolder.Stats.Defense.Value,player.PlayerFolder.Stats.Strength.Value,player.PlayerFolder.Stats.Health.Value,player.PlayerFolder.Stats.SLevel.Value,}
	Data:SetAsync(Key, Save)
end)

Here is the Punch Script[Local Script in Starterpack]

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Debounce = true
local HealthDebounce = true

script.Parent.Activated:connect(function()
	if not Debounce then return end
		Debounce = false
		Player.PlayerGui.Stats.StatsFrame.StrengthBar.Size = Player.PlayerGui.Stats.StatsFrame.StrengthBar.Size + UDim2.new(0,-2,0,0)
		Player.PlayerFolder.Stats.Strength.Value = Player.PlayerFolder.Stats.Strength.Value + 1 
		local A = Player.Character.Humanoid:LoadAnimation(script.one)
		A:Play()
		wait(.5)
		A:Stop()
		wait(.5)
		Debounce = true
end)	

1 answer

You need to use UpdateAsync instead of SetAsyc. The latter is prone to errors and overwriting existing data, while the former is much better as It better prevents outdated data, there are a few more reasons it's much better but I won't get into them.

TL; DR: Use SetAsync to create a datastore and use UpdateAsync to update an existing datastore. I'll have to look closer your code but it seems like it has to do with this.

This discussion is preserved as it was. New answers, comments, and votes are closed.