Trail Script not working

Hello! I am currently working on a script for my Trail Shop for the Upcoming Update for my game. I have tried several times but still I had no luck in making it work. I think the Table created for the Trails does not want to save and I still cannot find the issue in the code. Does anyone know how I can fix the problem?

Code:

local DataStoreService = game:GetService(“DataStoreService”)

local playerData = DataStoreService:GetDataStore(“playerData”)

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

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats

local trailsOwned = {}
pcall(function()
	trailsOwned = playerData:GetAsync("-trails" .. player.UserId) or {}
end)

local ChapterPlayStatus = Instance.new("Folder")
ChapterPlayStatus.Name = "ChapterPlayStatus"
ChapterPlayStatus.Parent = player

local Chapter1Play = Instance.new("BoolValue")
Chapter1Play.Value = false
Chapter1Play.Name = "Chapter1Play"
Chapter1Play.Parent = ChapterPlayStatus

local Chapter2Play = Instance.new("BoolValue")
Chapter2Play.Value = false
Chapter2Play.Name = "Chapter2Play"
Chapter2Play.Parent = ChapterPlayStatus

local data 
local success, errormessage = pcall(function()
	data = playerData:GetAsync(player.UserId.."-points") 
end)

local ownedFolder = Instance.new("Folder", player)
ownedFolder.Name = "OwnedTrails"


for i, owned in pairs(trailsOwned) do

	if trails:FindFirstChild(owned) then

		trails[owned]:Clone().Parent = ownedFolder
	end
end

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

player.CharacterAdded:Connect(createAtchs)

if success then 
	Points.Value = data
else
	print("Error while getting your data")
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local ownedTrails = {}
for i, trail in pairs(player.OwnedTrails:GetChildren()) do
	table.insert(ownedTrails, trail.Name)
end

local success, errormessage = pcall(function()
	playerData:SetAsync(player.UserId.."-points", player.leaderstats.Points.Value)
	playerData:SetAsync(player.UserId.."-trails", player.UserId, ownedTrails)
end)

if success then
	print("Data successfully saved!")
else
	print("There was an error while saving the data")
	warn(errormessage)
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 money = plr.leaderstats.Points

	if price <= money.Value then

		money.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)
<
Any ideas are greatly appreciated! (sorry about the code being cut into several parts idk how that happened)

1 Like

You accidently added another unnecessary player.UserId in :SetAsync.

New code:

--//Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local playerData = DataStoreService:GetDataStore("playerData")
local Trails = ReplicatedStorage:WaitForChild("Trails")

--//Functions
local function createAtchs(char)
	local hrp = char:WaitForChild("HumanoidRootPart")

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

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

local function SavePlayerData(player)
	local ownedTrails = {}

	for i, trail in pairs(player.OwnedTrails:GetChildren()) do
		table.insert(ownedTrails, trail.Name)
	end

	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.. "-points", player.leaderstats.Points.Value)
		playerData:SetAsync(player.UserId.. "-trails", ownedTrails)
	end)

	if not success then
		warn(errormessage)
	end
end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Value = playerData:GetAsync(player.UserId .."-points") or 0
	Points.Parent = leaderstats

	local trailsOwned = playerData:GetAsync("-trails".. player.UserId) or {}

	local ChapterPlayStatus = Instance.new("Folder")
	ChapterPlayStatus.Name = "ChapterPlayStatus"
	ChapterPlayStatus.Parent = player

	local Chapter1Play = Instance.new("BoolValue")
	Chapter1Play.Value = false
	Chapter1Play.Name = "Chapter1Play"
	Chapter1Play.Parent = ChapterPlayStatus

	local Chapter2Play = Instance.new("BoolValue")
	Chapter2Play.Value = false
	Chapter2Play.Name = "Chapter2Play"
	Chapter2Play.Parent = ChapterPlayStatus

	local ownedFolder = Instance.new("Folder")
	ownedFolder.Name = "OwnedTrails"
	ownedFolder.Parent = player

	local trailsOwned = playerData:GetAsync("-trails".. player.UserId) or {}

	for i, trailName in ipairs(trailsOwned) do
		local newTrail = Trails:WaitForChild(trailName):Clone()
		newTrail.Parent = ownedFolder
	end

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

	player.CharacterAdded:Connect(createAtchs)
end)

Players.PlayerRemoving:Connect(function(player)
	SavePlayerData(player)
end)

game:BindToClose(function()
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(SavePlayerData, player)
	end
end)

ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, trail)
	if not trail or typeof(trail) ~= "Instance" or not trail:IsA("Trail") then
		return
	end
	
	local ownsTrail = plr.OwnedTrails:FindFirstChild(trail.Name)
	
	if buying and not ownsTrail then
		local price = trail.Price.Value
		local money = plr.leaderstats.Points

		if price <= money.Value then
			money.Value -= price
			
			trail:Clone().Parent = plr.OwnedTrails
		end


	elseif not buying and ownsTrail 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)
1 Like

Thank you very much! You helped me a lot!

1 Like

No problem. If you have any more questions, feel free to ask.