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

Save Data has some errors, not sure if I did this right, help?

function saveScore(player, score)
	player:SaveNumber("Bounty", saveScore)
end
function saveScore2(player, score)
	player:SaveNumber("DevilFruit", saveScore)
end
function saveScore3(player, score)
	player:SaveNumber("Beli", saveScore)
end
function saveScore4(player, score)
	player:SaveNumber("KenHaki", saveScore)
end
function saveScore5(player, score)
	player:SaveNumber("BusoHaki", saveScore)
end
function saveScore6(player, score)
	player:SaveNumber("HaoHaki", saveScore)
end
function saveScore7(player, score)
	player:SaveNumber("Race", saveScore)
end
function saveScore8(player, score)
	player:SaveNumber("Geppo", saveScore)
end
function saveScore9(player, score)
	player:SaveNumber("Soru", saveScore)
end
function saveScore10(player, score)
	player:SaveNumber("Class", saveScore)
end
function loadScore(player, clickCounter)
	local score = player:LoadNumber("Bounty") 
	if score ~= 1 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end

function loadScore2(player, clickCounter)
	local score = player:LoadNumber("DevilFruit") 

	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end

function loadScore3(player, clickCounter)
	local score = player:LoadNumber("Beli") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore4(player, clickCounter)
	local score = player:LoadNumber("KenHaki") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore5(player, clickCounter)
	local score = player:LoadNumber("BusoHaki") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore6(player, clickCounter)
	local score = player:LoadNumber("HaoHaki") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore7(player, clickCounter)
	local score = player:LoadNumber("Race") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore8(player, clickCounter)
	local score = player:LoadNumber("Geppo") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore9(player, clickCounter)
	local score = player:LoadNumber("Soru") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function loadScore10(player, clickCounter)
	local score = player:LoadNumber("Class") 
	if score ~= 0 then
		clickCounter.Value = score
	else
		print("Nothing to load/score was 0")
	end
end
function onPlayerEntered(newPlayer)
		local stats = Instance.new("IntValue")
		stats.Name = "Data"
		local clicks = Instance.new("IntValue")
		clicks.Name = "Bounty"
		clicks.Value = 0
		local clicks2 = Instance.new("IntValue")
		clicks2.Name = "DevilFruit"
		clicks2.Value = 0
		local clicks3 = Instance.new("IntValue")
		clicks3.Name = "Beli"
		clicks3.Value = 0
		local clicks4 = Instance.new("IntValue")
		clicks4.Name = "KenHaki"
		clicks4.Value = 0
		local clicks5 = Instance.new("IntValue")
		clicks5.Name = "BusoHaki"
		clicks5.Value = 0
		local clicks6 = Instance.new("IntValue")
		clicks6.Name = "HaoHaki"
		clicks6.Value = 0
		local clicks7 = Instance.new("IntValue")
		clicks7.Name = "Race"
		clicks7.Value = 0
		local clicks8 = Instance.new("IntValue")
		clicks8.Name = "Geppo"
		clicks8.Value = 0
		local clicks9 = Instance.new("IntValue")
		clicks9.Name = "Soru"
		clicks9.Value = 0
		local clicks10 = Instance.new("IntValue")
		clicks10.Name = "Class"
		clicks10.Value = 0
		clicks.Parent = stats
		clicks2.Parent = stats
		clicks3.Parent = stats
		clicks4.Parent = stats
		clicks5.Parent = stats
		clicks6.Parent = stats
		clicks7.Parent = stats
		clicks8.Parent = stats
		clicks9.Parent = stats
		clicks10.Parent = stats
		stats.Parent = newPlayer
		newPlayer:WaitForDataReady()
		loadScore(newPlayer, clicks)
		loadScore2(newPlayer, clicks2)
		loadScore3(newPlayer, clicks3)
		loadScore4(newPlayer, clicks4)
		loadScore5(newPlayer, clicks5)
		loadScore6(newPlayer, clicks6)
		loadScore7(newPlayer, clicks7)
		loadScore8(newPlayer, clicks8)
		loadScore9(newPlayer, clicks9)
		loadScore10(newPlayer, clicks10)
end 
function onPlayerRespawned(player)
	print("Queso it works " .. player.Name)
	local stats = player:FindFirstChild("Data")
	if (stats ~= nil) then 
		local clicks = stats:FindFirstChild("Bounty")
		local clicks2 = stats:FindFirstChild("DevilFruit")
		local clicks3 = stats:FindFirstChild("Beli")
		local clicks4 = stats:FindFirstChild("KenHaki")
		local clicks5 = stats:FindFirstChild("BusoHaki")
		local clicks6 = stats:FindFirstChild("HaoHaki")
		local clicks7 = stats:FindFirstChild("Race")
		local clicks8 = stats:FindFirstChild("Geppo")
		local clicks9 = stats:FindFirstChild("Soru")
		local clicks10 = stats:FindFirstChild("Class")
		if (clicks ~= nil)and(clicks2 ~= nil)and(clicks3 ~= nil)and(clicks4 ~= nil)and(clicks5 ~= nil)and(clicks6 ~= nil)and(clicks7 ~= nil)and(clicks8 ~= nil)and(clicks9 ~= nil)and(clicks10 ~= nil)and(clicks11 ~= nil)and(clicks12 ~= nil)and(clicks13 ~= nil)and(clicks14 ~= nil)and(clicks15 ~= nil)and(clicks16 ~= nil)and(clicks17 ~= nil)and(clicks18 ~= nil)and(clicks19 ~= nil)and(clicks20 ~= nil) then
			saveScore(player, clicks.Value)
			saveScore2(player, clicks2.Value)
			saveScore3(player, clicks3.Value)
			saveScore4(player, clicks4.Value)
			saveScore5(player, clicks5.Value)
			saveScore6(player, clicks6.Value)
			saveScore7(player, clicks7.Value)
			saveScore8(player, clicks8.Value)
			saveScore9(player, clicks9.Value)
			saveScore10(player, clicks10.Value)
		end
	end
end
function onPlayerRemoving(player)
	print("Attempting to save score for " .. player.Name)
	local stats = player:FindFirstChild("Data")
	if (stats ~= nil) then 
		local clicks = stats:FindFirstChild("Bounty")
		local clicks2 = stats:FindFirstChild("DevilFruit")
		local clicks3 = stats:FindFirstChild("Beli")
		local clicks4 = stats:FindFirstChild("KenHaki")
		local clicks5 = stats:FindFirstChild("BusoHaki")
		local clicks6 = stats:FindFirstChild("HaoHaki")
		local clicks7 = stats:FindFirstChild("Race")
		local clicks8 = stats:FindFirstChild("Geppo")
		local clicks9 = stats:FindFirstChild("Soru")
		local clicks10 = stats:FindFirstChild("Class")
		if (clicks ~= nil)and(clicks2 ~= nil)and(clicks3 ~= nil)and(clicks4 ~= nil)and(clicks5 ~= nil)and(clicks6 ~= nil)and(clicks7 ~= nil)and(clicks8 ~= nil)and(clicks9 ~= nil)and(clicks10 ~= nil) then
              saveScore(player, clicks.Value)
			saveScore2(player, clicks2.Value)
			saveScore3(player, clicks3.Value)
			saveScore4(player, clicks4.Value)
			saveScore5(player, clicks5.Value)
			saveScore6(player, clicks6.Value)
			saveScore7(player, clicks7.Value)
			saveScore8(player, clicks8.Value)
			saveScore9(player, clicks9.Value)
			saveScore10(player, clicks10.Value)
			end
	end
end
game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)
while true do
	wait(150)
script.Disabled = true
wait()
script.Disabled = false
end

1 answer

✓ Accepted answer

I'm not going to go through 233 lines of unreadable code to find an issue that you haven't really explained.

What you've done in 233 lines can be done in 19.

local Stats = {"Bounty", "DevilFruit", "Beli", "KenHaki", 
				"BusoHaki", "HaoHaki", "Race", "Geppo", 
				"Soru", "Class"}

game.Players.PlayerAdded:connect(function(Player)
	if Player:WaitForDataReady() then
		local DataHolder = Instance.new("IntValue", Player)
		DataHolder.Name = "Data"
		for _, StatName in pairs(Stats) do
			local NewStat = Instance.new("IntValue", DataHolder)
			NewStat.Name = StatName
			NewStat.Value = Player:LoadNumber(StatName)
			NewStat.Changed:connect(function(NewValue)
				Player:SaveNumber(StatName, NewValue)
			end)
		end
	end
end)
  1. I've used a table to hold all of the value names so that I can loop through them and create each one without needing 250 lines (slight over-exaggeration) creating the same thing over and over again.

  2. I've used the Changed event to save changes to the values. Seeing as there are no restrictions to the amount of requests you can send (like DataStores), you might as well do that. You shouldn't do save data when the player is leaving seeing as that can lead to data loss/data not saving correctly/etc.

That about explains the code I've posted above.

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