So im trying to update datastore2 table when the script creates a new template but im not sure how to. Can someone help me?
local replicatedstorage = game:GetService("ReplicatedStorage")
local armormodule = require(game.ReplicatedStorage:WaitForChild("ArmorHandler"))
replicatedstorage.Events.UpdateInventory.OnClientEvent:Connect(function(player, armor)
local template = script.Parent.MainGui.Inventory.Templates.Template
local newTemplate = template:Clone()
newTemplate.Parent = script.Parent.MainGui.Inventory.Duplicates
newTemplate.Visible = true
local newArmor = armormodule.chooseRandom():Clone()
local camera = Instance.new("Camera")
camera.CFrame = CFrame.new(newArmor.PrimaryPart.Position + (newArmor.PrimaryPart.CFrame.lookVector * 3), newArmor.PrimaryPart.Position)
camera.Parent = newTemplate.ViewportFrame
newArmor.Parent = newTemplate.ViewportFrame
newTemplate.ViewportFrame.CurrentCamera = camera
-- I want the inventory to update here
end)
If needed heres the datahandler
local replicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
DataStore2.Combine("Data", "Inventory", "Currency", "Level", "Exp", "ExpToLevel")
local defaultLevel = 1
local defaultexp = 0
local DefaultCurrencyAmount = 0
local DefaultHealthAmount = 150
local defaultStrength = 10
local expToLevelUp = function(level)
return tonumber(100 * math.exp(level))
end
game.Players.PlayerAdded:Connect(function(player)
--All the DataStores in here--
local InventoryStore = DataStore2("Inventory", player)
local CurrencyStore = DataStore2("Currency", player)
local ExpStore = DataStore2("Exp", player)
local LevelStore = DataStore2("Level", player)
local replicatedDataFolder = Instance.new("Folder")
replicatedDataFolder.Parent = replicatedStorage.ReplicatedData
replicatedDataFolder.Name = player.UserId
---------------------------------------------
--STARTING THE INVENTORY FUNCTION--
local inventoryString = Instance.new("StringValue")
inventoryString.Parent = replicatedDataFolder
inventoryString.Name = "Inventory"
--Leaderstats--
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local LevelValue = Instance.new("IntValue", leaderstats)
LevelValue.Name = "Level"
local expValue = Instance.new("IntValue", leaderstats)
expValue.Name = "Exp"
local expToLevel = Instance.new("IntValue", leaderstats)
expToLevel.Name = "ExpToLevel"
local Warrior = Instance.new("IntValue",leaderstats)
Warrior.Name = "War"
Warrior.Value = defaultStrength
----------------------------------------------
--Functions--
local function updateLevel(amount)
player.leaderstats.Level.Value = amount
replicatedStorage.Events.UpdateClientLevel:FireClient(player, amount)
end
local function updateEXP(exp)
if exp >= expToLevelUp(LevelStore:Get(defaultLevel)) then
ExpStore:Increment(expToLevelUp(LevelStore:Get(defaultLevel)) * -1)
LevelStore:Increment(1)
game.Workspace["Level Up"]:Play()
game.StarterGui.Level.LevelUP.Visible = true
wait(1)
game.StarterGui.Level.LevelUP.Visible = false
else
player.leaderstats.Exp.Value = exp
end
local expAtNextLevel = expToLevelUp(LevelStore:Get(defaultLevel))
local currentlyHave = ExpStore:Get(defaultexp)
player.leaderstats.ExpToLevel.Value = expAtNextLevel - currentlyHave
end
local inventoryData = InventoryStore:Get({})
inventoryString.Value = HttpService:JSONEncode(inventoryData)
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)
--call functions right away one time
updateLevel(LevelStore:Get(defaultLevel))
updateEXP(ExpStore:Get(defaultexp))
LevelStore:OnUpdate(updateLevel)
ExpStore:OnUpdate(updateEXP)
end)