How could I make this table include variable "gold" when it can't be added to serverscript

READ WHOLE THING BEFORE READING SCRIPT AND VIDEO!! (btw please state where everything goes I’m not the best at assuming ;/) Anyways… I want to make a table (for opening a chest) include having the probability to give the variable I made “gold”. I will make script A the table with the probability and script B which is the script for the variable. I would also like it to be so that you can change script A (the table) could have different chances for amounts of gold (ex: 75% chance to get 500 gold 25% chance to get 2k gold) I will also provide the tutorial that explains script A (P.S. script C is a script for a menu after leveling up. Script B and C both refer to “gold”) I would recommend reading the scripts before the video of script A. Thank you for all of your time.
How To Make A Rarity Chest Loot System In Roblox Studio - YouTube.
Script A:

return {
	["ClassicSword"] = 60,
	["Flashlight"] = 30,
	["Handgun"] = 10,
}
-- This is just an example of how a table would look like. I can't find a way to
-- implement gold into the system because it's a variable not an item you can put
--in serverscript 

Script B:

--This is a script for the gold gui to show how I define it in other scripts
while wait(0.01) do
	script.Parent.Text = "Gold "..game.Players.LocalPlayer.Data.Gold.Value
end

Script C:

--I said this before but just to re-iterate this is the script where its a menu 
--where the stats go up with points. It also where it shows gold. I put other scripts just in case but I 
--probably prioritize the times they mention gold (ex: The dictionary at the beginning and the --gold part
local DataStore = game:GetService("DataStoreService")
local Level1 = DataStore:GetDataStore("Levels001")
local Beli11 = DataStore:GetDataStore("Beli001")
local Exp1 = DataStore:GetDataStore("Exp001")
local ExpNeed1 = DataStore:GetDataStore("ExpNeed001")
local DefenseP1 = DataStore:GetDataStore("DefenseP001")
local SwordP1 = DataStore:GetDataStore("SwordP001")
local LuckP1 = DataStore:GetDataStore("LuckP001")
local SpecialP1 = DataStore:GetDataStore("Special001")
local Defense1 = DataStore:GetDataStore("Defense001")
local Sword1 = DataStore:GetDataStore("Sword001")
local Luck1 = DataStore:GetDataStore("Luck001")
local Special1 = DataStore:GetDataStore("Special001")
local Points1 = DataStore:GetDataStore("Points001")

game.Players.PlayerAdded:Connect(function(Plr)
	local stats = Instance.new("Folder", Plr)
	stats.Name = "Data"
	--- Level System
	local Levels = Instance.new("IntValue", stats)
	Levels.Name = "Levels"
	Levels.Value = 1
	local Exp = Instance.new("IntValue", stats)
	Exp.Name = "Exp"
	Exp.Value = 0
	local ExpNeed = Instance.new("IntValue", stats)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 200
	--- Money System
	local Beli = Instance.new("IntValue", stats)
	Beli.Name = "Gold"
	Beli.Value = 0
	--- Stats Text
	local DefenseP = Instance.new("IntValue", stats)
	DefenseP.Name = "DefenseP"
	DefenseP.Value = 1
	local SwordP = Instance.new("IntValue", stats)
	SwordP.Name = "SwordP"
	SwordP.Value = 1
	local LuckP = Instance.new("IntValue", stats)
	LuckP.Name = "LuckP"
	LuckP.Value = 1
	local SpecialP = Instance.new("IntValue", stats)
	SpecialP.Name = "SpecialP"
	SpecialP.Value = 1
	--- Stats System
	local Points = Instance.new("IntValue", stats)
	Points.Name = "Points"
	Points.Value = 0
	local PointsS = Instance.new("IntValue", stats)
	PointsS.Name = "PointsS"
	PointsS.Value = 1
	local Defense = Instance.new("IntValue", stats)
	Defense.Name = "Defense"
	Defense.Value = 0
	local Sword = Instance.new("IntValue", stats)
	Sword.Name = "Sword"
	Sword.Value = 0
	local Luck = Instance.new("IntValue", stats)
	Luck.Name = "Luck"
	Luck.Value = 0
	local Special = Instance.new("IntValue", stats)
	Special.Name = "Special"
	Special.Value = 0
---- Datastore ----
--- Levels
   Levels.Value = Level1:GetAsync(Plr.UserId) or Levels.Value
	   Level1:SetAsync(Plr.UserId, Levels.Value)
      Levels.Changed:connect(function()
	   Level1:SetAsync(Plr.UserId, Levels.Value)
   end)
--- Gold
   Beli.Value = Beli11:GetAsync(Plr.UserId) or Beli.Value
	   Beli11:SetAsync(Plr.UserId, Beli.Value)
      Beli.Changed:connect(function()
	   Beli11:SetAsync(Plr.UserId, Beli.Value)
   end)
--- Exp
   Exp.Value = Exp1:GetAsync(Plr.UserId) or Exp.Value
	   Exp1:SetAsync(Plr.UserId, Exp.Value)
      Exp.Changed:connect(function()
	   Exp1:SetAsync(Plr.UserId, Exp.Value)
   end)
--- ExpNeed
   ExpNeed.Value = ExpNeed1:GetAsync(Plr.UserId) or ExpNeed.Value
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
      ExpNeed.Changed:connect(function()
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
   end)
--- SwordP
   SwordP.Value = SwordP1:GetAsync(Plr.UserId) or SwordP.Value
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
      SwordP.Changed:connect(function()
	   SwordP1:SetAsync(Plr.UserId, SwordP.Value)
   end)
--- DefenseP
   DefenseP.Value = DefenseP1:GetAsync(Plr.UserId) or DefenseP.Value
	   DefenseP1:SetAsync(Plr.UserId, DefenseP.Value)
      DefenseP.Changed:connect(function()
	   DefenseP1:SetAsync(Plr.UserId, DefenseP.Value)
   end)
--- LuckP
   ExpNeed.Value = ExpNeed1:GetAsync(Plr.UserId) or ExpNeed.Value
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
      ExpNeed.Changed:connect(function()
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
   end)
--- SpecialP
   ExpNeed.Value = ExpNeed1:GetAsync(Plr.UserId) or ExpNeed.Value
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
      ExpNeed.Changed:connect(function()
	   ExpNeed1:SetAsync(Plr.UserId, ExpNeed.Value)
   end)
--- Sword
   Sword.Value = Sword1:GetAsync(Plr.UserId) or Sword.Value
	   Sword1:SetAsync(Plr.UserId, SwordP.Value)
      Sword.Changed:connect(function()
	   Sword1:SetAsync(Plr.UserId, SwordP.Value)
   end)
--- Defense
   Defense.Value = Defense1:GetAsync(Plr.UserId) or Defense.Value
	   Defense1:SetAsync(Plr.UserId, Defense.Value)
      Defense.Changed:connect(function()
	   Defense1:SetAsync(Plr.UserId, Defense.Value)
   end)
--- Luck
   Luck.Value = Luck1:GetAsync(Plr.UserId) or Luck.Value
	   Luck1:SetAsync(Plr.UserId, Luck.Value)
      Luck.Changed:connect(function()
	   Luck1:SetAsync(Plr.UserId, Luck.Value)
   end)
--- Special
   Special.Value = Special1:GetAsync(Plr.UserId) or Special.Value
	   Special1:SetAsync(Plr.UserId, Special.Value)
      Special.Changed:connect(function()
	   Special1:SetAsync(Plr.UserId, Special.Value)
   end)
--- Points
   Points.Value = Points1:GetAsync(Plr.UserId) or Points.Value
	   Points1:SetAsync(Plr.UserId, Points.Value)
      Points.Changed:connect(function()
	   Points1:SetAsync(Plr.UserId, Points.Value)
   end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	wait(.1)
	local Exp = plr.Data.Exp
	local Levels = plr.Data.Levels
	local ExpNeed = plr.Data.ExpNeed
	local Points = plr.Data.Points
	
	while wait() do
		if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
			Levels.Value = Levels.Value + 1
			Points.Value = Points.Value + 3
			Exp.Value = Exp.Value - ExpNeed.Value
			ExpNeed.Value = ExpNeed.Value + 100
			game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
		end
	end
end)

game.Players.PlayerRemoving:connect(function(Player)
	Level1:SetAsync(Player.UserId, Player.Data.Levels.Value)
	Beli11:SetAsync(Player.UserId, Player.Data.Beli.Value)
	Exp1:SetAsync(Player.UserId, Player.Data.Exp.Value)
	ExpNeed1:SetAsync(Player.UserId, Player.Data.ExpNeed.Value)
	SwordP1:SetAsync(Player.UserId, Player.Data.SwordP.Value)
	DefenseP1:SetAsync(Player.UserId, Player.Data.DefenseP.Value)
	LuckP1:SetAsync(Player.UserId, Player.Data.LuckP.Value)
	SpecialP1:SetAsync(Player.UserId, Player.Data.SpecialP.Value)
	Sword1:SetAsync(Player.UserId, Player.Data.Sword.Value)
	Defense1:SetAsync(Player.UserId, Player.Data.Defense.Value)
	Luck1:SetAsync(Player.UserId, Player.Data.Luck.Value)
	Special1:SetAsync(Player.UserId, Player.Data.Special.Value)
	Points1:SetAsync(Player.UserId, Player.Data.Points.Value)
end)
 

There everybody reload I fixed it. Thanks again!

Btw when I said fixed I mean I fixed the link. This is the closest I could get by myself

local goldaward1 = Data.Gold.Value + 50
return {
	["ClassicSword"] = 5,
	["Flashlight"] = 5,
	["goldaward1"] = 90,
-- the problem right now is Data is an unknown global. Data is the name of Script 
--C how would I fix this?
}

Btw if there are any ways to make this script less choppy please revise. Thanks again again

Update: I might of said it wrong but the table (script A) is in server storage. To see all the scripts of the chest click the video!

(This is not an answer to your current issue, but will have an impact later on)

There are datastore limits that prevent you from calling / setting datastores too often. Because of this, you should combine all the datastores into a single one, accessible by a single key per player. Making use of a dictionary will help keep all the individual parts seperate and easily accessible.

Instead of

local Level1 = DataStore:GetDataStore("Levels001")
local Beli11 = DataStore:GetDataStore("Beli001")
local Exp1 = DataStore:GetDataStore("Exp001")
local ExpNeed1 = DataStore:GetDataStore("ExpNeed001")
local DefenseP1 = DataStore:GetDataStore("DefenseP001")
local SwordP1 = DataStore:GetDataStore("SwordP001")
local LuckP1 = DataStore:GetDataStore("LuckP001")
local SpecialP1 = DataStore:GetDataStore("Special001")
local Defense1 = DataStore:GetDataStore("Defense001")
local Sword1 = DataStore:GetDataStore("Sword001")
local Luck1 = DataStore:GetDataStore("Luck001")
local Special1 = DataStore:GetDataStore("Special001")
local Points1 = DataStore:GetDataStore("Points001")

you should only need to have

local Datastore = DataStore:GetDataStore("Data")

An example dictionary could be

local UserData = {
    ["Level"] = 5,
    ["Exp"] = 5512,
    ["Beli"] = 55,
    ["Sword"] = "CoolSword",
    ["Defense"] = 1,
    ["Luck"] = 3,
}

Then use HTTPService:JSONEncode() and HTTPService:JSONDecode() to change it to and from a string, which is savable in datastores.

Doing this will help prevent being rate limited, which will result in dataloss and hence very unhappy players.

1 Like

@PoppyandNeivaarecute Quick question real quick. What is that dictionary for? Is this what you get after reaching a certain level?

The dictionary allows you store different stats that the player has in a single table, allowing it to be stored in a single datastore, rather than a bunch.
(I was short on time so I didn’t make the dictionary that extensive last night, but it would look something like this:

local UserData = {
    ["Level"] = 5,
    ["Exp"] = 5512,
    ["Beli"] = 55,
    ["ExpNeed"] = 6912,
    ["Sword"] = "CoolSword",
    ["Defense"] = 5,
    ["Luck"] = 3,
    ["Special"] = 5,
    ["Points"] = 589,
}

You would store the player’s information in the dictionary called UserData. Then using HTTPService JSONDecode and JSONEncode you convert it to and from a string. That string is then stored in a datastore using just :SetAsync()

I’ll see if I can address the original point of this topic…

1 Like

I perfectly understand if not but can you make the script so I could just copy and paste it in the script. Figuring stuff like how to use those type of services are kind of confusing. Any resource that would teach me how to do that would be amazing (normally I probably could figure it out myself but right now I am focused on something else in the project :P)