Here’s the Output
Look at the red highlighted areas
21:20:00.669 'Created folder PurchasedEmotes for player LUFCarebest - Server - Newinvmanager:15'
21:20:00.669 Created folder PurchasedSpells for player LUFCarebest - Server - Newinvmanager:15
21:20:00.907 Loaded emote: Emote1 with quantity: 14 - Server - Newinvmanager:227
21:20:00.907 Loaded emote: Emote2 with quantity: 1 - Server - Newinvmanager:227
21:20:00.908 Loaded emote: Emote3 with quantity: 1 - Server - Newinvmanager:227
21:20:00.908 Loaded spell: spell7 with quantity: 12 - Server - Newinvmanager:253
21:20:00.908 Loaded spell: spell8 with quantity: 1 - Server - Newinvmanager:253
21:20:00.908 Loaded spell: spell16 with quantity: 1 - Server - Newinvmanager:253
21:20:25.144 Attempting to use item: Emote1 - Server - Newinvmanager:80
21:20:25.144 'LUFCarebest does not have any PurchasedEmotes folder. - Server - Newinvmanager:105'
21:20:25.144 LUFCarebests children: - Server - Newinvmanager:106
21:20:25.144 - StarterGear - Server - Newinvmanager:108
21:20:25.145 - PlayerGui - Server - Newinvmanager:108
21:20:25.145 - Data - Server - Newinvmanager:108
21:20:25.145 - leaderstats - Server - Newinvmanager:108
21:20:25.145 - PurchasedEmotes - Server - Newinvmanager:108
21:20:25.145 - PurchasedSpells - Server - Newinvmanager:108
21:20:25.145 - Backpack - Server - Newinvmanager:108
Here’s the code - Its long
The main function to look at will be the player initialisation and HandleUse.
Bare in mind the PurchasedSpells works flawlessly.
local DataStoreService = game:GetService("DataStoreService")
local purchaseStore = DataStoreService:GetDataStore("PlayerPurchases")
local purchaseEvent = game.ReplicatedStorage.Remotes:WaitForChild("PurchaseItem")
local loadoutEditEvent = game.ReplicatedStorage.Remotes:WaitForChild("Loadoutedit")
local updateItemQuantityEvent = game.ReplicatedStorage.Remotes:WaitForChild("UpdateItemQuantity")
-- Function to create a folder in the player to store purchased items if it doesn't exist
local function createFolderIfNotExist(player, folderName)
local folder = player:FindFirstChild(folderName)
if not folder then
folder = Instance.new("Folder")
folder.Name = folderName
folder.Parent = player
print("Created folder " .. folderName .. " for player " .. player.Name)
end
return folder
end
-- Function to handle purchases
local function handlePurchase(player, itemName, itemCost, itemType, imageId)
local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
-- Check if player has enough coins
if coins.Value >= itemCost then
-- Deduct the cost
coins.Value = coins.Value - itemCost
print("Player " .. player.Name .. " bought " .. itemName .. " for " .. itemCost .. " coins.")
-- Ensure the correct folder exists
local folder
if itemType == "Emote" then
folder = createFolderIfNotExist(player, "PurchasedEmotes")
elseif itemType == "Spell" then
folder = createFolderIfNotExist(player, "PurchasedSpells")
end
-- Check if the item already exists in the folder
local item = folder:FindFirstChild(itemName)
if item then
-- If it exists, increase the quantity
local quantityValue = item:FindFirstChild("Quantity")
if quantityValue then
quantityValue.Value = quantityValue.Value + 1
print("Increased quantity of " .. itemName .. " to " .. quantityValue.Value .. ".")
end
else
-- If it doesn't exist, create a new item folder
local newItemFolder = Instance.new("Folder")
newItemFolder.Name = itemName
-- Create a StringValue for the name
local nameValue = Instance.new("StringValue")
nameValue.Name = "Name"
nameValue.Value = itemName
nameValue.Parent = newItemFolder
-- Create an IntValue for the quantity
local quantityValue = Instance.new("IntValue")
quantityValue.Name = "Quantity"
quantityValue.Value = 1 -- Start with 1 since it was just purchased
quantityValue.Parent = newItemFolder
-- Create an IntValue for the image ID
local idValue = Instance.new("IntValue")
idValue.Name = "ID"
idValue.Value = imageId -- Store the image ID
idValue.Parent = newItemFolder
newItemFolder.Parent = folder
print("Added " .. itemName .. " to folder " .. folder.Name .. " for player " .. player.Name)
end
else
warn(player.Name .. " does not have enough coins to buy " .. itemName)
end
end
-- Function to handle using an item (Emote or Spell)
local function handleUse(player, itemName, itemType)
print("Attempting to use item: " .. itemName) -- Debugging line
local folder
-- Identify whether it's a Spell or an Emote
if itemType == "Spell" then
folder = player:FindFirstChild("PurchasedSpells")
print("Attempting to access PurchasedSpells: " .. tostring(folder))
if not folder then
folder = player:WaitForChild("PurchasedSpells", 5) -- Wait for the folder to exist
end
print(player.Name .. " is attempting to use a spell.")
elseif itemType == "Emote" then
print("Player name is: " .. player.Name) -- Confirm player instance
wait(1) -- Add a delay for debugging
folder = player:FindFirstChild("PurchasedEmotes")
print("Checking for PurchasedEmotes after wait: " .. tostring(folder)) -- Debug line
if not folder then
folder = player:WaitForChild("PurchasedEmotes", 5) -- Wait for the folder to exist
end
print(player.Name .. " is attempting to use an emote.")
end
-- DEBUGGING: Print the folder state
if not folder then
warn(player.Name .. " does not have any " .. (itemType == "Spell" and "PurchasedSpells" or "PurchasedEmotes") .. " folder.")
print(player.Name .. "'s children:")
for _, child in pairs(player:GetChildren()) do
print(" - " .. child.Name) -- This will print the player's direct children
end
return
else
print("Found folder: " .. folder.Name)
end
local itemToUse = folder:FindFirstChild(itemName)
-- DEBUG: Check if the item is found in the folder
if not itemToUse then
warn(player.Name .. " tried to use an item that doesn't exist: " .. itemName)
return
else
print("Found item: " .. itemToUse.Name)
end
-- If the item is found, proceed with using it
local quantityValue = itemToUse:FindFirstChild("Quantity")
if quantityValue then
if quantityValue.Value > 0 then
quantityValue.Value = quantityValue.Value - 1
print(player.Name .. " used 1 of " .. itemName .. ". Remaining: " .. quantityValue.Value)
-- Fire the update event to the player for UI updates
updateItemQuantityEvent:FireClient(player, itemName, quantityValue.Value)
if quantityValue.Value <= 0 then
itemToUse:Destroy()
print(player.Name .. " has no more " .. itemName .. " left, removing it.")
end
else
warn(player.Name .. " tried to use " .. itemName .. " but quantity is 0.")
end
else
warn("Quantity value is missing for item: " .. itemName)
end
end
-- Function to handle updating item quantity from the client
local function handleUpdateItemQuantity(player, itemName, newQuantity, itemType)
local folder = player:FindFirstChild(itemType == "Spell" and "PurchasedSpells" or "PurchasedEmotes")
if folder then
local item = folder:FindFirstChild(itemName)
if item then
local quantityValue = item:FindFirstChild("Quantity")
if quantityValue then
quantityValue.Value = newQuantity -- Update the quantity
print(player.Name .. " updated " .. itemName .. " quantity to " .. newQuantity)
-- Optionally, save the updated quantity to the DataStore here
local purchases = {
Emotes = {},
Spells = {}
}
-- Update emotes
for _, emote in pairs(player.PurchasedEmotes:GetChildren()) do
local quantity = emote:FindFirstChild("Quantity")
if quantity then
table.insert(purchases.Emotes, {Name = emote.Name, Quantity = quantity.Value})
end
end
-- Update spells
for _, spell in pairs(player.PurchasedSpells:GetChildren()) do
local quantity = spell:FindFirstChild("Quantity")
if quantity then
table.insert(purchases.Spells, {Name = spell.Name, Quantity = quantity.Value})
end
end
purchaseStore:SetAsync(player.UserId, purchases)
print("Saved updated quantity of " .. itemName .. " for player " .. player.Name .. " to DataStore.")
end
end
end
end
-- Listen for purchase event from the client
purchaseEvent.OnServerEvent:Connect(handlePurchase)
-- Listen for use event from the client
loadoutEditEvent.OnServerEvent:Connect(function(player, itemName, itemType)
handleUse(player, itemName, itemType)
end)
-- Listen for item quantity update from the client
updateItemQuantityEvent.OnServerEvent:Connect(function(player, itemName, newQuantity, itemType)
handleUpdateItemQuantity(player, itemName, newQuantity, itemType)
end)
-- Function to initialize the player's data
local function initializePlayerData(player)
local emotesFolder = createFolderIfNotExist(player, "PurchasedEmotes")
local spellsFolder = createFolderIfNotExist(player, "PurchasedSpells")
-- Load purchases from the DataStore or set defaults
local purchases = purchaseStore:GetAsync(player.UserId) or {Emotes = {}, Spells = {}}
-- Populate the player's PurchasedEmotes and PurchasedSpells folders
for _, emoteData in pairs(purchases.Emotes) do
local emoteName = emoteData.Name
local quantity = emoteData.Quantity
local emote = Instance.new("Folder")
emote.Name = emoteName
local nameValue = Instance.new("StringValue")
nameValue.Name = "Name"
nameValue.Value = emoteName
nameValue.Parent = emote
local quantityValue = Instance.new("IntValue")
quantityValue.Name = "Quantity"
quantityValue.Value = quantity -- Load the actual quantity
quantityValue.Parent = emote
emote.Parent = emotesFolder
print("Loaded emote: " .. emoteName .. " with quantity: " .. quantity)
end
for _, spellData in pairs(purchases.Spells) do
local spellName = spellData.Name
local quantity = spellData.Quantity
local spell = Instance.new("Folder")
spell.Name = spellName
local nameValue = Instance.new("StringValue")
nameValue.Name = "Name"
nameValue.Value = spellName
nameValue.Parent = spell
local quantityValue = Instance.new("IntValue")
quantityValue.Name = "Quantity"
quantityValue.Value = quantity -- Load the actual quantity
quantityValue.Parent = spell
-- Load the image ID from the DataStore if available
local imageIdValue = Instance.new("IntValue")
imageIdValue.Name = "ID"
imageIdValue.Value = spellData.ID or 0 -- Set a default ID if not present
imageIdValue.Parent = spell
spell.Parent = spellsFolder
print("Loaded spell: " .. spellName .. " with quantity: " .. quantity)
end
end
-- Load player purchases when they join the game
game.Players.PlayerAdded:Connect(function(player)
initializePlayerData(player)
end)
-- Save purchases when the player leaves the game
game.Players.PlayerRemoving:Connect(function(player)
local purchases = {
Emotes = {},
Spells = {}
}
-- Save emotes
for _, emote in pairs(player.PurchasedEmotes:GetChildren()) do
local quantity = emote:FindFirstChild("Quantity")
if quantity then
table.insert(purchases.Emotes, {Name = emote.Name, Quantity = quantity.Value})
end
end
-- Save spells
for _, spell in pairs(player.PurchasedSpells:GetChildren()) do
local quantity = spell:FindFirstChild("Quantity")
if quantity then
local idValue = spell:FindFirstChild("ID") -- Get the ID value
local id = idValue and idValue.Value or 0 -- Default to 0 if ID is not found
table.insert(purchases.Spells, {Name = spell.Name, Quantity = quantity.Value, ID = id}) -- Save ID too
end
end
purchaseStore:SetAsync(player.UserId, purchases)
print("Saved player purchases to DataStore.")
end)