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

How to save/load this IntValues?

How could i add add a save/load script to this?

local GoodBoy = script.EXPNeeded.Value

game.Players.PlayerAdded:connect(function(plr)
 local stats = Instance.new("IntValue", plr)
 stats.Name = "leaderstats"
 local EXP = Instance.new("IntValue", stats)
 EXP.Name = "EXP"
 EXP.Value = 0
 local level = Instance.new("IntValue", stats)
 level.Name = "Level"
 level.Value = 0
 local Souls = Instance.new("IntValue", stats)
 Souls.Name = "Souls"
 Souls.Value = 0
 
 EXP.Changed:connect(function()
  level.Value = math.floor(EXP.Value / script.EXPNeeded)
  EXP.Value = 0
  script.GoodBoy = GoodBoy + 10
  end)
end)

2 comments

1

You use datastore

0

i already knew it but how?

1 answer

I highly recommend you check this out: http://wiki.roblox.com/index.php?title=Saving_Player_Data

Here's the script with some explanations: (Make sure your game has access to API Services, or else DataStore won't work)

DataStore = game:GetService("DataStoreService"):GetDataStore("Stats")--Name whatever you want
local GoodBoy = script.EXPNeeded.Value
SaveTime = 60 -- Saves every (SaveTime) seconds (I recommend not going under 60 seconds...)

game.Players.PlayerAdded:Connect(function(plr)
	local PlayerKey = "user_".. plr.userId -- Sets a key for the Player's stored data
	local stats = Instance.new("IntValue", plr)
	stats.Name = "leaderstats"
	if DataStore:GetAsync(PlayerKey)then --This function loads data with the Player's key. If there is no data, then it will retrun nul. So here we are checking if there is data
		print("Loading " .. PlayerKey)
		local Data = DataStore:GetAsync(PlayerKey) -- Loads our saved Table
		print(Data.Souls)
		local EXP = Instance.new("IntValue", stats)
		EXP.Name = "EXP"
		EXP.Value = Data.EXP
		local level = Instance.new("IntValue", stats)
		level.Name = "Level"
		level.Value = Data.level
		local Souls = Instance.new("IntValue", stats)
		Souls.Name = "Souls"
		Souls.Value = Data.Souls
	else do -- Setups new data
		local NewStats = {EXP = 0,level = 0,Souls = 0,} --This Table saves all our Variable's Data
		local EXP = Instance.new("IntValue", stats)
		EXP.Name = "EXP"
		EXP.Value = 0
		local level = Instance.new("IntValue", stats)
		level.Name = "Level"
		level.Value = 0
		local Souls = Instance.new("IntValue", stats)
		Souls.Name = "Souls"
		Souls.Value = 0
		DataStore:SetAsync(PlayerKey,NewStats) -- This saves our Table
	end
	end
plr.leaderstats.EXP.Changed:Connect(function() --Not sure what this is... but it's in your original script sooo I left it here...
  	plr.leaderstats.Level.Value = math.floor(plr.leaderstats.EXP.Value/script.EXPNeeded.Value) --Added .Value
  	plr.leaderstats.EXP.Value = 0
  	GoodBoy = GoodBoy + 10 --Removed script.GoodBoy
  end)
while true do --Save loop
	wait(SaveTime)
	local PlayerKey = "user_".. plr.userId -- Sets a key for the Player's stored data
	local Stats = plr.leaderstats
	local Data = {}
	Data.EXP = Stats.EXP.Value
	Data.level = Stats.Level.Value
	Data.Souls = Stats.Souls.Value
	DataStore:SetAsync(PlayerKey,Data)
	print("Saved Data")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player) -- This will save the Player's data when leaving the game
	local PlayerKey = "user_".. Player.userId -- Sets a key for the Player's stored data
	local Stats = Player.leaderstats
	local Data = {}
	Data.EXP = Stats.EXP.Value
	Data.level = Stats.Level.Value
	Data.Souls = Stats.Souls.Value
	DataStore:SetAsync(PlayerKey,Data) -- Saves our Data Table
end)

I also recommend you read through here: https://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore (That has everything you need to know about Datastore.)

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