#Issue where I can not access data from the player inventory consistently.
1 messages · Page 1 of 1 (latest)
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
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)```
the uh function where you check if they can sell or whatever
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
Oh yeah
send that snippet
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
don't add a space
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)```
print("Hello world")
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
alright, what about the function related to selling
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
you're trying to call OnServerInvoke on a RemoteEvent
you can only call that on RemoteFunctions
same with InvokeServer in the script above
Should I just make that a remote function then
upto you
depend on what you need it to do
I think you should though based on your script above
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)
elseprofile: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
are you sure you're using profileservice right
yeah
print(profile.Data.Inventory)?
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
yeah they're broken rn
@frozen lion "It isn't a script" when it litearlly is a script idk bro
FindFirstChild("EndGame")
local endGame = SS:FindFirstChild("EndGame")
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
game:GetService("ProfileService") won't do anything
it's not a true service, it's just called that
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 😭
local profile = player.UserId won't work nor will the GetService("ProfileService")