#Issue where I can not access data from the player inventory consistently.

1 messages · Page 1 of 1 (latest)

ebon nova
#

I am pretty sure my data is not being set up correctly since it is such a pain trying to get it and to adjust it from the user.

#

Getting the error again idk why it wont let me send

#

this is rough

frozen lion
#

could you send the function or the snippet where you award the player the item for killing the npc

#

and then send the other thing

ebon nova
#

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local player = game.Players.LocalPlayer

local playerInventory = {}

local function onPlayerWon()
    Remotes.PlayerWon:FireServer()
end

local function spawnWeapon(weapon)
    local character = player.Character
    if not character then return end

    -- Find the weapon Tool in ReplicatedStorage.Tools
    local weaponToolTemplate = game.ReplicatedStorage.Tools:FindFirstChild(weapon.name)

    if not weaponToolTemplate then
        print("Weapon tool not found in ReplicatedStorage.Tools")
        return
    end

    -- Clone the weapon Tool and set its name and parent
    local weaponTool = weaponToolTemplate:Clone()
    weaponTool.Parent = player.Backpack

    local damageValue = Instance.new("IntValue")
    damageValue.Name = "Damage"
    damageValue.Value = weapon.damage
    damageValue.Parent = weaponTool
end

local function onUpdateInventory(newInventory)
    playerInventory = newInventory
    print("Updated inventory for " .. player.Name .. ".")
end
-- Listen for the PlayerWon and UpdateInventory remote events
Remotes.PlayerWon.OnClientEvent:Connect(onPlayerWon)
Remotes.UpdateInventory.OnClientEvent:Connect(onUpdateInventory)```
frozen lion
#

the uh function where you check if they can sell or whatever

ebon nova
#

This is the local script called "PlayerWonClient"

#

and I was trying to get the sell to work, but it wouldn't let me acsess the inventory at all

frozen lion
#

whenever you send the script could you do this:

ebon nova
#

Oh yeah

ebon nova
#

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local player = game.Players.LocalPlayer

local playerInventory = {}

local function onPlayerWon()
    Remotes.PlayerWon:FireServer()
end

local function spawnWeapon(weapon)
    local character = player.Character
    if not character then return end

    -- Find the weapon Tool in ReplicatedStorage.Tools
    local weaponToolTemplate = game.ReplicatedStorage.Tools:FindFirstChild(weapon.name)

    if not weaponToolTemplate then
        print("Weapon tool not found in ReplicatedStorage.Tools")
        return
    end

    -- Clone the weapon Tool and set its name and parent
    local weaponTool = weaponToolTemplate:Clone()
    weaponTool.Parent = player.Backpack

    local damageValue = Instance.new("IntValue")
    damageValue.Name = "Damage"
    damageValue.Value = weapon.damage
    damageValue.Parent = weaponTool
end

local function onUpdateInventory(newInventory)
    playerInventory = newInventory
    print("Updated inventory for " .. player.Name .. ".")
end
-- Listen for the PlayerWon and UpdateInventory remote events
Remotes.PlayerWon.OnClientEvent:Connect(onPlayerWon)
Remotes.UpdateInventory.OnClientEvent:Connect(onUpdateInventory)```
#

did that work

#

let me re-write it, i deleted it lmao

frozen lion
#

don't add a space

ebon nova
#

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local player = game.Players.LocalPlayer

local playerInventory = {}

local function onPlayerWon()
    Remotes.PlayerWon:FireServer()
end

local function spawnWeapon(weapon)
    local character = player.Character
    if not character then return end

    -- Find the weapon Tool in ReplicatedStorage.Tools
    local weaponToolTemplate = game.ReplicatedStorage.Tools:FindFirstChild(weapon.name)

    if not weaponToolTemplate then
        print("Weapon tool not found in ReplicatedStorage.Tools")
        return
    end

    -- Clone the weapon Tool and set its name and parent
    local weaponTool = weaponToolTemplate:Clone()
    weaponTool.Parent = player.Backpack

    local damageValue = Instance.new("IntValue")
    damageValue.Name = "Damage"
    damageValue.Value = weapon.damage
    damageValue.Parent = weaponTool
end

local function onUpdateInventory(newInventory)
    playerInventory = newInventory
    print("Updated inventory for " .. player.Name .. ".")
end
-- Listen for the PlayerWon and UpdateInventory remote events
Remotes.PlayerWon.OnClientEvent:Connect(onPlayerWon)
Remotes.UpdateInventory.OnClientEvent:Connect(onUpdateInventory)```
frozen lion
#
print("Hello world")
ebon nova
#

I saw it turned green

#
-- PlayerWonClient LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local player = game.Players.LocalPlayer

local playerInventory = {}

local function onPlayerWon()
    Remotes.PlayerWon:FireServer()
end

local function spawnWeapon(weapon)
    local character = player.Character
    if not character then return end

    -- Find the weapon Tool in ReplicatedStorage.Tools
    local weaponToolTemplate = game.ReplicatedStorage.Tools:FindFirstChild(weapon.name)

    if not weaponToolTemplate then
        print("Weapon tool not found in ReplicatedStorage.Tools")
        return
    end

    -- Clone the weapon Tool and set its name and parent
    local weaponTool = weaponToolTemplate:Clone()
    weaponTool.Parent = player.Backpack

    local damageValue = Instance.new("IntValue")
    damageValue.Name = "Damage"
    damageValue.Value = weapon.damage
    damageValue.Parent = weaponTool
end

local function onUpdateInventory(newInventory)
    playerInventory = newInventory
    print("Updated inventory for " .. player.Name .. ".")
end
-- Listen for the PlayerWon and UpdateInventory remote events
Remotes.PlayerWon.OnClientEvent:Connect(onPlayerWon)
Remotes.UpdateInventory.OnClientEvent:Connect(onUpdateInventory)
#

Ohhhh

#

thats sick

frozen lion
#

alright, what about the function related to selling

ebon nova
#

Let me redo it

#
-- SellItems LocalScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local player = game.Players.LocalPlayer

local proximityPrompt = script.Parent

-- Function to sell all items in the player's inventory
local function sellAllItems()
    local coins = Remotes.GetCoins:InvokeServer(player)
    local inventory = {}

    -- Iterate through all the player's inventory items and sell them
    for _, item in ipairs(player.Character.Data.Inventory:GetChildren()) do
        coins = coins + item.Value
        item:Destroy()
    end

    -- Update the player's coin count and inventory
    Remotes.AdjustCoins:FireServer(coins)
    Remotes.UpdateInventory:FireServer(inventory)
end

-- Connect the sellAllItems function to the ProximityPrompt
proximityPrompt.Triggered:Connect(sellAllItems)
#

now I am getting errors unlucky

frozen lion
#

you're trying to call OnServerInvoke on a RemoteEvent

#

you can only call that on RemoteFunctions

ebon nova
#

Oh

#

I did not know that

frozen lion
#

same with InvokeServer in the script above

ebon nova
#

Should I just make that a remote function then

frozen lion
#

upto you

#

depend on what you need it to do

#

I think you should though based on your script above

ebon nova
#

This is what my tempalte looks like ```lua
local skills = {

Stamina = 0,
Magic = 0,
Power = 0,
ToUse = 0

}

local user = {

Level = 0,
Experience = 0,
Coins = 0,

Skills = skills,

Inventory = {}

}

return user

#

for some reason it is a pain being able to acsess the inc

#

inv*

#

Like I wanted to just print the inventory where I commented, but I couldnt get acsess to it. ```lua

  • When player joins the game
    local function PlayerAdded(player)

    -- Store profile of the user
    local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)

    -- Kick the user if player does not load
    if profile == nil then player:Kick(errorMsg) return end

    -- Get user information
    profile:AddUserId(player.UserId)
    profile:Reconcile()

    -- Server error
    profile:ListenToRelease(function()
    Manager.Profiles[player] = nil
    player:Kick(errorMsg)
    end)

    -- Add user to the list
    if player:IsDescendantOf(Players) == true then
    Manager.Profiles[player] = profile
    GiveLeaderstats(player)
    else

      profile:Release()
    

    end
    --- here I was just gonna print the inv

end
Players.PlayerAdded:Connect(PlayerAdded)

for _, player in Players:GetPlayers() do
task.spawn(PlayerAdded, player)
end

frozen lion
#

are you sure you're using profileservice right

ebon nova
#

yeah

frozen lion
#

print(profile.Data.Inventory)?

ebon nova
#

oh wow that worked

#

wtf

#

ngl

#

hmm

#

I was over complicating it

#

i guess

#

i was trying to use a for loop

#

I switched it to a remote function but I don't think its that easy considering it gave me the error ```lua
17:05:02.434 InvokeServer can only be called from the client - Server - Sell:11

#

which was ```lua
local coins = Remotes.GetCoins:InvokeServer(player)

#

I am just gonna remove the stuff that I do not understand correctly so I can re-go through it

#

oh bruh

frozen lion
#

yeah they're broken rn

ebon nova
#

oh okay

#

i thought I did smth

ebon nova
#

@frozen lion "It isn't a script" when it litearlly is a script idk bro

frozen lion
#

FindFirstChild("EndGame")

ebon nova
#

Oh

#

true

frozen lion
#

local endGame = SS:FindFirstChild("EndGame")

ebon nova
#

What the flip

#

I passed it through as a peram

#

@frozen lion I am not great with server to client stuff but I feel like this makes sense

frozen lion
#

game:GetService("ProfileService") won't do anything

#

it's not a true service, it's just called that

ebon nova
#

Ohhh

#

fart

#

idk anyore

#

anymore*

#

maybe I shouldnt use profile service

#

it seems to be making it harder for me idk

#

Wait this won't work either then - but It was saving earlier? ```lua
-- WeaponDrops module
local WeaponDrops = {}

-- Weapon templates
local weapons = {
{name = "Sword", minDamage = 10, maxDamage = 20},
{name = "Axe", minDamage = 15, maxDamage = 25},
{name = "Bow", minDamage = 8, maxDamage = 18},
}

-- Function to generate random weapon
local function generateRandomWeapon()
local selectedWeapon = weapons[math.random(#weapons)]
local weaponInstance = {
name = selectedWeapon.name,
damage = math.random(selectedWeapon.minDamage, selectedWeapon.maxDamage)
}
return weaponInstance
end

-- Event that is triggered when a player wins
function WeaponDrops.onPlayerWin(player)
-- Get the player's profile
local profile = player.UserId

-- Generate a new weapon
local newWeapon = generateRandomWeapon()
print("Player won! Reward: " .. newWeapon.name .. " with damage " .. newWeapon.damage)

-- Add the new weapon to the player's inventory
table.insert(profile.Data.Inventory, newWeapon)

-- Save the updated profile
game:GetService("ProfileService"):Save(profile)

end

return WeaponDrops

#

I think it would work until the last line @frozen lion

#

I had it working earlier idk what happen 😭

frozen lion
#

local profile = player.UserId won't work nor will the GetService("ProfileService")

ebon nova
#

is there a pre-configured one from Profule service?

#

Profile*

#

I think i will just need to look at more profileservice videos lol @frozen lion

#

Its like I can acsess the data directly which is