- What do you want to achieve? Keep it simple and clear!
I’m trying to fix my module inventory script. The problem is when i return the data to the player it wont update reliably until you update it twice or multiple times.
Module Script
local inventory = {playerlist = {}}
local playerlist = inventory.playerlist
function inventory.AddPlayer(player)
playerlist[player] = {
inventory = {}
}
end
function inventory.ReturnPlayer(player)
return playerlist[player]
end
function inventory.AddItem(player, itemname, itemtype)
local playerinventory = playerlist[player].inventory
if playerinventory[itemname] then
print("Item Found")
playerinventory[itemname].amount += 1
else
local newitem = {
itemname = itemname,
itemtype = itemtype,
amount = 1
}
playerinventory[itemname] = newitem
end
print(playerinventory)
end
function inventory.RemoveItem(player, itemname)
local playerinventory = playerlist[player].inventory
if playerinventory[itemname].amount <= 1 then
playerinventory[itemname] = nil
return
end
playerinventory[itemname].amount -= 1
end
return inventory
Sever Script
local remoteevent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local inventorymodule = require(game.ReplicatedStorage.Inventory)
game.Players.PlayerAdded:Connect(function(player)
inventorymodule.AddPlayer(player)
end)
remoteevent.OnServerEvent:Connect(function(player)
remoteevent:FireClient(player, inventorymodule.ReturnPlayer(player))
end)
Local Script
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local inventory
local remoteevent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local uips = game:GetService("UserInputService")
function setUpPlayer()
remoteevent:FireServer()
end
function UpdateInventory(inventoryData)
print("Inventory Data ", inventoryData)
inventory = inventoryData
--print(inventory)
end
remoteevent.OnClientEvent:Connect(UpdateInventory)
setUpPlayer()
uips.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.H then
remoteevent:FireServer()
print(player.Name.. " Called Inventory ", inventory)
end
end)
when you press H to print the inventory it sometimes shows its empty until you press it twice or multiple times even though InventoryData is updated which should set inventory to InventoryData from the local script.
the blue arrow is where I press H the first time, the red arrow is where I pressed H to print the inventory the 2nd time