Hello, I’ve made a Weapons Crate that gives out a random weapon taken out of ReplicatedStorage, everything is working except there is one problem. The crate is supposed to emit particles and then destroy itself after giving the player a weapon, but when there are multiple crates the particles play on a different crate and destroys that crate instead of the one the player is opening.
Here is the script for the crate:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local prompt = script.Parent.Parent.ProximityPrompt
local ItemsFolder = ReplicatedStorage:WaitForChild("Weapons"):GetChildren()
local Crate = script.Parent.Parent.Parent.Parent.WeaponCrate
local Particles = Crate.WoodEffect
prompt.PromptButtonHoldBegan:Connect(function()
Particles.Enabled = true
end)
prompt.PromptButtonHoldEnded:Connect(function()
Particles.Enabled = false
end)
prompt.Triggered:Connect(function(player)
local Item = ItemsFolder[math.random(1, #ItemsFolder)]:Clone()
Item.Parent = player.Backpack
Crate:Destroy()
end)
There were no errors in the output and I thought that it would be different if I didn’t include the Name of the model at the end of the Crate variable but it still doesn’t work, and it doesn’t work at all if I do that.
It sounds like the issue is that you are using the same variable, ‘Crate’, to refer to different instances of the ‘WeaponCrate’ object in your game. This is causing the particles to play on the wrong crate when the player opens it.
To fix this issue, you could modify your script to use a different variable to refer to the specific ‘WeaponCrate’ instance that the player is interacting with. Here is an example of how you could possibly do that:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local prompt = script.Parent.Parent.ProximityPrompt
local ItemsFolder = ReplicatedStorage:WaitForChild("Weapons"):GetChildren()
local Crate = script.Parent.Parent.Parent.Parent.WeaponCrate
local Particles = Crate.WoodEffect
prompt.PromptButtonHoldBegan:Connect(function()
-- Get the specific instance of WeaponCrate that the player is interacting with
local activeCrate = prompt.Parent.Parent.Parent.Parent.WeaponCrate
-- Enable the particles on the active crate
activeCrate.WoodEffect.Enabled = true
end)
prompt.PromptButtonHoldEnded:Connect(function()
-- Get the specific instance of WeaponCrate that the player is interacting with
local activeCrate = prompt.Parent.Parent.Parent.Parent.WeaponCrate
-- Disable the particles on the active crate
activeCrate.WoodEffect.Enabled = false
end)
prompt.Triggered:Connect(function(player)
-- Get the specific instance of WeaponCrate that the player is interacting with
local activeCrate = prompt.Parent.Parent.Parent.Parent.WeaponCrate
local Item = ItemsFolder[math.random(1, #ItemsFolder)]:Clone()
Item.Parent = player.Backpack
-- Destroy the active crate
activeCrate:Destroy()
end)