Need help on this script

Hello, so I have two scripts in my game that allow players to buy trails and equip them. The problem is that each time they die they have to re-equip the trail. How do I fix this or what should I add to my script to fix this.
Here are the scripts:

The first script:

local dss = game:GetService(“DataStoreService”)
local ds = dss:GetDataStore(“DATA”)

local trails = game.ReplicatedStorage:WaitForChild(“Trails”)

function createAtchs(char)

local hrp = char:WaitForChild("HumanoidRootPart")

local atchTop = Instance.new("Attachment", hrp)
atchTop.Name = "TrailTop"
atchTop.Position = Vector3.new(0, 0.766, 0)

local atchBtm = Instance.new("Attachment", hrp)
atchBtm.Name = "TrailBottom"
atchBtm.Position = Vector3.new(0, -0.766, 0)

end

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", plr)
ownedFolder.Name = "OwnedTrails"


for i, owned in pairs(trailsOwned) do
	
	if trails:FindFirstChild(owned) then
		
		trails[owned]:Clone().Parent = ownedFolder
	end
end

if plr.Character then createAtchs(plr.Character) end

plr.CharacterAdded:Connect(createAtchs)

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
	end
	
	
elseif not buying and plr.OwnedTrails:FindFirstChild(trail.Name) then
	
	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)

The second script:

local shopFrame = script.Parent:WaitForChild(“ShopFrame”)
local invFrame = script.Parent:WaitForChild(“InventoryFrame”)

local trailsFolder = game.ReplicatedStorage:WaitForChild(“Trails”)
local ownedTrailsFolder = game.Players.LocalPlayer:WaitForChild(“OwnedTrails”)

function updateInventory()

for i, child in pairs(invFrame.TrailsScroller:GetChildren()) do

	if child:IsA("ImageLabel") then child:Destroy() end
end


local ownedTrails = ownedTrailsFolder:GetChildren()

table.sort(ownedTrails, function(a, b)
	return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
end)


for i, trail in pairs(ownedTrails) do

	local item = script.Item:Clone()
	item.SelectButton.Text = "EQUIP"
	item.TrailName.Text = trail.Name
	item.Trail.UIGradient.Color = trail.Color

	item.Parent = invFrame.TrailsScroller


	if game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"):FindFirstChild(trail.Name) then
		item.SelectButton.Text = "EQUIPPED"
	end


	item.SelectButton.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.TrailSelectedRE:FireServer(false, trail)

		for i, itemButton in pairs(item.Parent:GetChildren()) do

			if itemButton:IsA("ImageLabel") then
				itemButton.SelectButton.Text = "EQUIP"
			end
		end
		item.SelectButton.Text = "EQUIPPED"
	end)
end

end

function updateShop()

for i, child in pairs(shopFrame.Frame1Effects.TrailsScroller:GetChildren()) do

	if child:IsA("ImageLabel") then child:Destroy() end
end


local shopTrails = trailsFolder:GetChildren()

table.sort(shopTrails, function(a, b)
	return a.Price.Value < b.Price.Value or a.Price.Value == b.Price.Value and a.Name < b.Name
end)


for i, trail in pairs(shopTrails) do

	local item = script.Item:Clone()

	item.SelectButton.Text = "BUY for " .. trail.Price.Value
	if ownedTrailsFolder:FindFirstChild(trail.Name) then item.SelectButton.Text = "Owned." end

	item.TrailName.Text = trail.Name
	item.Trail.UIGradient.Color = trail.Color

	item.Parent = shopFrame.Frame1Effects.TrailsScroller


	item.SelectButton.MouseButton1Click:Connect(function()
		game.ReplicatedStorage.TrailSelectedRE:FireServer(true, trail)
	end)
end

end

updateShop()
updateInventory()

ownedTrailsFolder.ChildAdded:Connect(function()
updateInventory(); updateShop()
end)
ownedTrailsFolder.ChildRemoved:Connect(function()
updateInventory(); updateShop()
end)

trailsFolder.ChildAdded:Connect(updateShop)
trailsFolder.ChildRemoved:Connect(updateShop)

1 Like

The thing is that every time they die, their character clears. So everytime they die, you have to paste the trail back into them.

1 Like

Yep, you could probably create a string value in the Player, then check for that value in the characteradded function and if you find a trail name in that value, do this again:

local newTrail = trail:Clone()

	newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
	newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom

	newTrail.Parent = char.HumanoidRootPart
1 Like

Oh ok i’ll try this and tell you if it works.