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

DataStore Sometimes Not Saving?

The Datastore I made Was For a Game I am Working On But Sometimes Its Not Saving The Data. I Tried Everything But I Could Find A Way So Thats Why I Am Asking Here. There Is Also A Properties Folder Containing XPPerLevel , AdditionalXPNeeded , LevelCap , XPExponentMagnitude , XPPerLevel.

local Properties = game.ReplicatedStorage.LevelUpProperties

function onXPChanged(player, XP, level) 
	if string.lower(Properties.XPType.Value) == "linear" then
		if XP.Value>= Properties.XPPerLevel.Value * (level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then 
			XP.Value = XP.Value - (Properties.XPPerLevel.Value*(level.Value) + Properties.AdditionalXPNeeded.Value) 
			level.Value = level.Value + 1 
		end 		
	end
end 

function SaveData(Player)
	if Player.DataReady == false then
		print("Could not save, DataReady is false.")
		return
	end
	--Stats
	local Stats = Player.LevelingSystem:GetChildren()
	print("Saving Data For "..Player.Name)
	local datastore = game:GetService("DataStoreService"):GetDataStore(Player.Name.."Stats")

	local statstorage = Player:FindFirstChild("LevelingSystem"):GetChildren()
	for i , v in pairs(statstorage) do
		datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
		print("saved data number "..i)
	end
	print("Data has been saved successfully for " .. Player.Name .. "!")
end

function LoadData(Player)
	if Player.DataReady == false then
		warn("Could not load, DataReady is false.")
		return
	end
	--Stats
	local Stats = Player.LevelingSystem:GetChildren()
	local datastore = game:GetService("DataStoreService"):GetDataStore(Player.Name.."Stats")

	Player:WaitForChild("LevelingSystem")
	wait(1)
	local stats = Player:FindFirstChild("LevelingSystem"):GetChildren()
	for i , v in pairs(stats) do			
		stats[i].Value = datastore:GetAsync(stats[i].Name)
		print("stat number "..i.." has been found")
	end
end

function onPlayerEntered(newPlayer)

		local stats = Instance.new("Folder")
		stats.Name = "LevelingSystem"
		
		local misc = Instance.new("Folder")
		misc.Name = "Stats"

		--Leaderstats variables
	local clicks = Instance.new("NumberValue", stats)
		clicks.Name = "Lvl"
		clicks.Value = 1

	local clicks2 = Instance.new("NumberValue", stats)
		clicks2.Name = "XP"
	    clicks2.Value = 0
	
	local clicks3= Instance.new("NumberValue", stats)
	    clicks3.Name = "Money" -- Change "Money" with your currency.
	    clicks3.Value = 0		    
	
	   local clicks4 = Instance.new("BoolValue", stats)
	   clicks4.Name = "EggHunter"
	   clicks4.Value = false
	
	local clicks5 = Instance.new("BoolValue", stats)
	clicks5.Name = "PreAlpha"
	clicks5.Value = false
	
	local clicks6 = Instance.new("StringValue", stats)
	clicks6.Name = "Rank"
	clicks6.Value = ""
	
	local clicks7 = Instance.new("NumberValue", stats)
	clicks7.Name = "Missions"
	clicks7.Value = 1
	
	local clicks8 = Instance.new("NumberValue", stats)
	clicks8.Name = "Time"
	clicks8.Value = 1
	
	local clicks9 = Instance.new("BoolValue", stats)
	clicks9.Name = "Banned"
	clicks9.Value = false
	
		--Assign the folders to player.
	stats.Parent = newPlayer
	misc.Parent = newPlayer
	
	    clicks9.Changed:connect(function(newPlayer, clicks9) end)
	    clicks8.Changed:connect(function(newPlayer, clicks8) end)
	    clicks7.Changed:connect(function(newPlayer, clicks7) end)
	    clicks6.Changed:connect(function(newPlayer, clicks6) end)
	    clicks5.Changed:connect(function(newPlayer, clicks5) end)
	    clicks4.Changed:connect(function(newPlayer, clicks4) end)
	    clicks3.Changed:connect(function(newPlayer, clicks3) end) 
	    clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end) 
	    clicks.Changed:connect(function(newPlayer, clicks2, clicks) end)
	
            newPlayer:WaitForDataReady()
	    LoadData(newPlayer)

end

function onPlayerRemoving(player)
	print("Attempting to save score for " .. player.Name)
	SaveData(player)
end

game.Players.PlayerRemoving:Connect(onPlayerRemoving)
game.Players.PlayerAdded:Connect(onPlayerEntered)

2 comments

0

I hate data store stuff -.- I'm sorry but I'm not going to read it all ... I will just say, look up "BindToClose()" it might help ????????

0

I Tried "BindToClose()" And I Have The Same Problem

1 answer

✓ Accepted answer

you're trying to use the old (deprecated) way to load data while saving with DataStoreService, use DataStoreService for saving and loading

also use ipairs/pairs (pairs if you're looping through a dictionary) to loop through tables

4 comments

0

ok but can you show me the new way to do it because i dont know how

0

you should learn yourself with the official wiki (https://developer.roblox.com/articles/Data-store), modify the LoadData function to work with DataStoreService

0

Thanks

0

no problem

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