Cloned part's proximity prompt doesn't work

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)
2 Likes

You could create a new proximity prompt, like:

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)
1 Like

Could you share the full context of the code? Such as how you are placing the duffel bag in.

I copied over your code to a blank baseplate and it works just fine for me, it prints 1.

local Bag = game.ReplicatedStorage["Duffel Bag"]:Clone()

Bag.PrimaryPart.Position = Vector3.new(0,10,0)
Bag.Parent = workspace

Bag.ProximityPrompt.Triggered:Connect(function()
	print(1)
	Bag:Destroy()
end)

The problem is probably being caused by a part of the code you haven’t already shared with us.

1 Like

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)
1 Like

this doesn’t seem to work sadly

1 Like

There is the issue, the system must be a server script. The current system only works with the player who died

2 Likes

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.

Hope this helps! :slight_smile:

4 Likes