Im trying to make an inventory system, and when a person dies, I want them to drop their inventory
However, when I clone a part from replicated storage and put it where the player’s root part when they die, the part appears but when the prompt is triggered, nothing happens. how come?
script:
local Bag = game.ReplicatedStorage["Duffel Bag"]:Clone()
Bag.ProximityPrompt.Triggered:Connect(function() -- never runs
print(1) -- or this never appears
for _, v in pairs(Bag:GetChildren()) do
print(2)
if v:IsA("IntValue") then
print(3)
for _, x in pairs(Player.Inventory) do
print(4)
if x.Name == v.Name then
print(5)
x.Value = x.Value + v.Value
v:Destroy()
end
end
end
end
Bag:Destroy()
end)
local Bag = game.ReplicatedStorage["Duffel Bag"]:Clone()
local prompt = Instance.new("ProximityPrompt", Bag)
prompt.Triggered:Connect(function() -- never runs
print(1) -- or this never appears
for _, v in pairs(Bag:GetChildren()) do
print(2)
if v:IsA("IntValue") then
print(3)
for _, x in pairs(Player.Inventory) do
print(4)
if x.Name == v.Name then
print(5)
x.Value = x.Value + v.Value
v:Destroy()
end
end
end
end
Bag:Destroy()
end)
heres the full script, maybe it could be the event? not too sure, still looking
(local script btw)
local Bag = game.ReplicatedStorage["Duffel Bag"]:Clone()
local Player = game:GetService("Players").LocalPlayer
Player.Character:WaitForChild("Humanoid").Died:Connect(function()
print(Player.Name, "died")
Bag.Parent = workspace
Bag.Position = Player.Character.HumanoidRootPart.Position
Bag.Anchored = false
for _, v in pairs(Player.Inventory:GetChildren()) do
if v:IsA("IntValue") then
local IntClone = v:Clone()
IntClone.Parent = Bag
end
end
for _, v in pairs(Player.Inventory:GetChildren()) do
if v:IsA("IntValue") then
v.Value = 0
end
end
task.wait(2)
Bag.ProximityPrompt.ActionText = Player.Name.."'s Inventory"
Bag.ProximityPrompt.Enabled = true
print(Bag.Parent)
end)
Bag.ProximityPrompt.Triggered:Connect(function()
print(1)
for _, v in pairs(Bag:GetChildren()) do
print(2)
if v:IsA("IntValue") then
print(3)
for _, x in pairs(Player.Inventory) do
print(4)
if x.Name == v.Name then
print(5)
x.Value = x.Value + v.Value
v:Destroy()
end
end
end
end
Bag:Destroy()
end)
Found the issue, you’re only cloning the bag once. You need to clone it every time a player dies. I’ve reworked the code to fix this issue.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local function BagTriggered(Bag)
print(1)
for _, v in pairs(Bag:GetChildren()) do
print(2)
if v:IsA("IntValue") then
print(3)
for _, x in pairs(Player.Inventory:GetChildren()) do
print(4)
if x.Name == v.Name then
print(5)
x.Value = x.Value + v.Value
v:Destroy()
end
end
end
end
Bag:Destroy()
end
Character:WaitForChild("Humanoid").Died:Connect(function()
local Bag = game.ReplicatedStorage:WaitForChild("Duffel Bag"):Clone()
print(Player.Name, "died")
Bag.Parent = workspace
Bag.Position = Character.HumanoidRootPart.Position
Bag.Anchored = false
for _, v in pairs(Player.Inventory:GetChildren()) do
if v:IsA("IntValue") then
local IntClone = v:Clone()
IntClone.Parent = Bag
end
end
for _, v in pairs(Player.Inventory:GetChildren()) do
if v:IsA("IntValue") then
v.Value = 0
end
end
wait(2)
Bag.ProximityPrompt.ActionText = Player.Name.."'s Inventory"
Bag.ProximityPrompt.Enabled = true
print(Bag.Parent)
Bag.ProximityPrompt.Triggered:Connect(function()
BagTriggered(Bag)
end)
end)
Edit: Make sure you place the local script under StarterCharacterScripts, otherwise this code will break. If it is of great importance that the script stays under StarterPlayerScripts I can rewrite it to account for this issue.