Trail not attaching

Hello so the script below is part of a trail system I have in my game and it attaches the trail to the player each time they respawn. The only problem is when the player first loads in and clicks the equip button the trail does not attach to the player until they die. After that the script great. Any ideas what the problem might be or how I can fix this?

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("DATA")
local trails = game.ReplicatedStorage:WaitForChild("Trails")

function saveData(plrLeaving)
	local ownedTrails = {}
	for i, trail in pairs(plrLeaving.OwnedTrails:GetChildren()) do
		table.insert(ownedTrails, trail.Name)
	end
	local success, err = pcall(function()
		ds:SetAsync("trails-" .. plrLeaving.UserId, ownedTrails)
		ds:SetAsync("Coins-" .. plrLeaving.UserId, plrLeaving.leaderstats.Coins.Value)
	end)
end

game.Players.PlayerAdded:Connect(function(plr)
	local trailsOwned = {}
	pcall(function()
		trailsOwned = ds:GetAsync("trails-" .. plr.UserId) or {}
	end)
	local ownedFolder = Instance.new("Folder")
	ownerFolder.Parent = plr
	ownedFolder.Name = "OwnedTrails"
	for i, owned in pairs(trailsOwned) do
		if trails:FindFirstChild(owned) then
			trails[owned]:Clone().Parent = ownedFolder
		end
	end
	plr.CharacterAdded:Connect(function(char)
		local hrp = char:WaitForChild("HumanoidRootPart")
		local atchTop = Instance.new("Attachment")
		atchTop.Parent = hrp
		atchTop.Name = "TrailTop"
		atchTop.Position = Vector3.new(0, 0.766, 0)
		local atchBtm = Instance.new("Attachment")
		atchBtm.Parent = hrp
		atchBtm.Name = "TrailBottom"
		atchBtm.Position = Vector3.new(0, -0.766, 0)
		if plr:FindFirstChild("LastTrail") then
			local newTrail = plr:FindFirstChild("LastTrail").Value:Clone()
			newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
			newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
			newTrail.Parent = char.HumanoidRootPart
		end
	end)
end)

game.Players.PlayerRemoving:Connect(saveData)

game:BindToClose(function()
	for i, plrLeaving in pairs(game.Players:GetPlayers()) do
		saveData(plrLeaving)
	end
end)

game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, trail)
	if buying and not plr.OwnedTrails:FindFirstChild(trail.Name) then
		local price = trail.Price.Value
		local coins = plr.leaderstats.Coins
		if price <= coins.Value then
			coins.Value -= price
			trail:Clone().Parent = plr.OwnedTrails
			local char = plr.Character
			if not char or not char:FindFirstChild("HumanoidRootPart") then return end
			for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
				if child:IsA("Trail") then child:Destroy() end
			end
			local newTrail = trail:Clone()
			newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
			newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
			newTrail.Parent = char.HumanoidRootPart
		end
	elseif not buying and plr.OwnedTrails:FindFirstChild(trail.Name) then
		if plr:FindFirstChild("LastTrail") then
			plr:FindFirstChild("LastTrail").Value = trail
		else
			local obj = Instance.new("ObjectValue")
			obj.Parent = plr
			obj.Name = "LastTrail"
			obj.Value = trail
		end
		local char = plr.Character
		if not char or not char:FindFirstChild("HumanoidRootPart") then return end
		for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
			if child:IsA("Trail") then child:Destroy() end
		end
		local newTrail = trail:Clone()
		newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
		newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
		newTrail.Parent = char.HumanoidRootPart
	end
end)
2 Likes

The problem could be being caused by this line:

if plr:FindFirstChild("LastTrail") then

Maybe the player only gets the LastTrail object after they have died at least once.
Add an elseif statement creating the trail.

I don’t know how your system works exactly, so it’s difficult for me to provide help on this issue.

1 Like