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

Where is the bug in this script that's not making the currency amounts save?


I was following an Alvinblox tutorial, but I made changes to the data store script to make it work with 2 currency's, I've manged to debug most of it, but for some reason it isn't working when I tested in the public test game.

I think it's this --->

if savedData ~= nil then currency.Value = savedData print("Data Loaded") else

part of the script that's not working correctly, since it's not printing "Data Loaded" I decided to post the whole script just in case it's not this part.

local currencyName = "Loot" local currencyName2 = "Gold"

--Data store has been created-- local DataStore = game:GetService("DataStoreService"):GetDataStore("LootandGoldTestingData")

print("Loot and Gold datastore created ^^ ")

--Currency folder has been created-- game.Players.PlayerAdded:Connect(function(player) ~~~~~~~~~~~~~~~~~


	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	local currency2 = Instance.new("IntValue")
	currency.Name = currencyName
	currency2.Name = currencyName2
	currency.Parent = folder
	currency2.Parent = folder
	
	
	print("Loot and gold leaderboard/currency folder has been created ^^ ")
	
	local ID = currencyName.."-"..player.UserId
	local ID = currencyName2.."-"..player.UserId
	local savedData = nil
	
	
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		currency.Value = savedData
		print("Data Loaded")
	else
		-- New player
		currency.Value = 0
		currency2.Value = 100
		print("New player has joined the game")
	end
end)



game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	local ID = currencyName2.."-"..player.UserId
	DataStore:GetAsync(ID,player.leaderstats[currencyName].Value)
	DataStore:GetAsync(ID,player.leaderstats[currencyName2].Value)
end)



game:BindToClose(function()
	
	--When game is ready to shut down--
	for i, player in pairs(game.Players:GetPlayers())do
		if player then 
			player:Kick("This server is shutting down")
		end
	end
	wait(5)
end)





(If this script is ACTUALLY correct it might be that I'm testing this wrong.)

2 comments

0

make all scripts code block

0

how?

1 answer

I am going to give you an example script with two currency values saving:

First make sure you have enabled studio access to API services

Here is the example script:

local datastore = game:GetService("DataStoreService") -- Define DataStoreService
local darealdatastore = datastore:GetDataStore("ValueData") -- Make a DataStore


-- The following is to be added at the end of the PlayerAdded function 
local moneyData -- Defining Money Data
local currency2Data -- Defining Currency2 Data

local success,err = pcall(function() [[-- pcall is short for Protected Call. It calls another function in a protected setting. This means that if there were any errors in the function, it would not stop the rest of the code from running and would prevent a game error --]]

      moneyData = darealdatastore:GetAsync(plr.UserId .. "-Money") or 0

      print("Money Data locked and loaded") --lol
end)

if success then -- If the above was a success then 
      Money.Value = moneyData -- Set the value of Money to moneyData
else -- Or else
      print("Could not load money data") -- Print this for testing
      warn(err) -- Warn the error caused
end

local success1,erro = pcall(function() [[-- I put success1 and erro as we already defined success and err above. You cannot write error as it is a Lua thingy. --]]

      currency2Data = darealdatastore:GetAsync(plr.UserId .."-currency") or 0 [[-- Set swipes data to the one that is stored in key. If there is none then it will set it to zero--]]

      print("yoink data locked and loaded") -- Print for testing
end)

if success1 then -- If success then
      currency.Value = currency2Data -- Set currency value to currency2Data
else -- Or else
      print("Could not load swipes data!!!") -- Testing
      warn(erro) -- Warn
end


game.Players.PlayerRemoving:Connect(function(plr) -- When a player leaves game
      local success1,erro = pcall(function() 
         darealdatastore:SetAsync(plr.UserId .. "-Money", plr.leaderstats.Money.Value) [[-- SetAsync this time to save the leaderstats data to the key.--]]
      end)

     if success1 then
           print("Money daved")
     else
           print("Error saving data")
           warn(erro)
     end

      local success,err = pcall(function()
           darealdatastore:SetAsync(plr.UserId .. "-currency", plr.leaderstats.currency.Value)
      end)

      if success then
            print("currency Data Saved")
      else
            print("Data save error")
            warn(err)
      end
end)

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