Trail Attachment Bug

Hello.

I have a shop where you can purchase trails in exchange for a certain amount of my game’s currency. If you own a trail, you can equip it. Only one trail can be equipped at a time.

The system itself works fine.

I’m just having issues when I try to keep the trail attached to the player even after they die or reset their character. I’ve already tried using CharacterAdded, but I’m not sure if I’m using it correctly (in terms of trails). My code is below, and I’ve only included the relevant parts.

Code
local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
DataStore2.Combine("key", "rebirths", "inventory", "equipped_item")

local rDefault = 0
local invDefault = {}
local eiDefault = ""

game.Players.PlayerAdded:Connect(function(player)
	
	local character = player.Character or player.CharacterAdded:Wait()
	
	local function addTrailAttachments(trail)
		
		if character:FindFirstChild("Head"):FindFirstChildWhichIsA("Trail") then
			local x = character:FindFirstChild("Head"):FindFirstChildWhichIsA("Trail")
			x.Attachment0:Destroy()
			x.Attachment1:Destroy()
			x:Destroy()
		end
		
		trail.Parent = character:FindFirstChild("Head")
		
		local attachment0 = Instance.new("Attachment")
		attachment0.Name = "attachment0"
		attachment0.Parent = character:FindFirstChild("Head")

		local attachment1 = Instance.new("Attachment")
		attachment1.Name = "attachment1"
		attachment1.Parent = character:FindFirstChild("HumanoidRootPart")

		trail.Attachment0 = attachment0
		trail.Attachment1 = attachment1
		
	end
	
	-- [[ other code ]] --
	
	local equippedItem = Instance.new("StringValue")
	equippedItem.Name = "EquippedItem"
	equippedItem.Parent = player
	
	-- [[ other code ]] --

	local EquippedItemData = DataStore2("equipped_item", player)
	
	-- [[ other code ]] --
	
	local function updateEquippedItem(newString)
		
		equippedItem.Value = newString
		
		for i, v in pairs(character:GetDescendants()) do
			if v:IsA("Trail") then
				v:Destroy()
			end
		end
		
		if game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value) then
			local equippedTrail = game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value):Clone()
			addTrailAttachments(equippedTrail)
		end
		
	end

	-- [[ other code ]] --
	
	updateEquippedItem(EquippedItemData:Get(eiDefault))

    -- [[ other code ]] --
	
	EquippedItemData:OnUpdate(updateEquippedItem)
	
	-- [[ other code ]] --
	
	player.CharacterAdded:Connect(function()
		
		for i, v in pairs(character:GetDescendants()) do
			if v:IsA("Trail") then
				v:Destroy()
			end
		end

		if game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value) then
			local equippedTrail = game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value):Clone()
			addTrailAttachments(equippedTrail)
		end
		
	end)
	
end)

Any help would be greatly appreciated. Thank you!

1 Like
local DataStore2 = require(game.ServerScriptService:WaitForChild("DataStore2"))
DataStore2.Combine("key", "rebirths", "inventory", "equipped_item")

local rDefault = 0
local invDefault = {}
local eiDefault = ""

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
	
	local function addTrailAttachments(trail)
		
		if character:FindFirstChild("Head"):FindFirstChildWhichIsA("Trail") then
			local x = character:FindFirstChild("Head"):FindFirstChildWhichIsA("Trail")
			x.Attachment0:Destroy()
			x.Attachment1:Destroy()
			x:Destroy()
		end
		
		trail.Parent = character:FindFirstChild("Head")
		
		local attachment0 = Instance.new("Attachment")
		attachment0.Name = "attachment0"
		attachment0.Parent = character:FindFirstChild("Head")

		local attachment1 = Instance.new("Attachment")
		attachment1.Name = "attachment1"
		attachment1.Parent = character:FindFirstChild("HumanoidRootPart")

		trail.Attachment0 = attachment0
		trail.Attachment1 = attachment1
		
	end
	
	-- [[ other code ]] --
	
	local equippedItem = Instance.new("StringValue")
	equippedItem.Name = "EquippedItem"
	equippedItem.Parent = player
	
	-- [[ other code ]] --

	local EquippedItemData = DataStore2("equipped_item", player)
	
	-- [[ other code ]] --
	
	local function updateEquippedItem(newString)
		
		equippedItem.Value = newString
		
		for i, v in pairs(character:GetDescendants()) do
			if v:IsA("Trail") then
				v:Destroy()
			end
		end
		
		if game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value) then
			local equippedTrail = game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value):Clone()
			addTrailAttachments(equippedTrail)
		end
		
	end

	-- [[ other code ]] --
	
	updateEquippedItem(EquippedItemData:Get(eiDefault))

    -- [[ other code ]] --
	
	EquippedItemData:OnUpdate(updateEquippedItem)
	
	-- [[ other code ]] --
	
	player.CharacterAdded:Connect(function()
		
		for i, v in pairs(character:GetDescendants()) do
			if v:IsA("Trail") then
				v:Destroy()
			end
		end

		if game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value) then
			local equippedTrail = game.ReplicatedStorage.Items:FindFirstChild(equippedItem.Value):Clone()
			addTrailAttachments(equippedTrail)
		end
		
	end)
	end)
end)

To properly use character added you need to do

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
end)
end)

I’ve already tried putting the function there, but it’s instancing my folders and values every time I reset (since the code for it is in the CharacterAdded function).

This is problematic. I’m trying to save some values to a data store. With multiple instances, my data is being inconsistent.

Is there a better location for the CharacterAdded function in my script? I don’t want it to meddle with anything else.

EDIT: I solved the problem on my own after moving the CharacterAdded function to different places. I defined character as player.Character when I created the trail for the first time, and I used the character argument in the CharacterAdded function that recreated the trail (if it was equipped) every time the character was reset.

you could do something like

character.Humanoid:GetPropertyChangedSignal:Connect(function()
 if character.Humanoid.Health <= 0 then
-- code
end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.