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)