Hello.
I am having trouble making a trail shop for my game. The issue is that it should make a folder names "leaderstats" in the user's stuff where all the folders are but it wont, instead it makes a folder in "Players" section and i havent found a fix for it. I have tried a ton of different ways on how to fix this but yet none worked.
Here's the code Error should be in line 46
-- Variables
local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local player = game:GetService('Players').PlayerAdded:Wait()
local dataStore = game:GetService("DataStoreService"):GetDataStore("TrailShopTest")
local Players = game:GetService("Players")
--Functions
local function EquipTrail(plr,trail)
local char = plr.Character
if trail ~= nil and char ~= nil then
if char.HumanoidRootPart:FindFirstChild("Attachment") then char.HumanoidRootPart.Attachment:Destroy() end
if char.Head:FindFirstChild("Attachment") then char.Head.Attachment:Destroy() end
if char:FindFirstChild(plr.Name.."'s Trail") then char[plr.Name.."'s Trail"]:Destroy() end
trail.Name = plr.Name.."'s Trail"
local attachment1 = Instance.new("Attachment",char:FindFirstChild("HumanoidRootPart"))
local attachment2 = Instance.new("Attachment",char:FindFirstChild("Head"))
trail.Attachment0 = attachment1
trail.Attachment1 = attachment2
trail.Parent = char
end
end
Players.PlayerAdded:Connect(function(Player)
local UserId = Player.UserId
local Currency = CurrencyStore:GetAsync(UserId)
if Currency == nil then
Currency = 0
CurrencyStore:SetAsync(UserId, Currency)
end
-- Player Joins
game.player.PlayerAdded:Connect(function(player)
-- Stats for the player
local Leaderstats = Instance.new("Folder",game.Players.LocalPlayer)
Leaderstats.Name = "leaderstats"
local coins = Instance.new("IntValue", Leaderstats)
coins.Name = "Coins"
coins.Value = Currency
coins.Changed:Connect(function(NewValue)
CurrencyStore:SetAsync(UserId, NewValue)
end)
end)
local trailInventory = Instance.new("Folder",player); trailInventory.Name = "TrailInventory"
local equippedTrail = Instance.new("StringValue",player); equippedTrail.Name = "EquippedTrail"
-- Player's character added to the game
player.CharacterAdded:Connect(function(char)
if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
end
end)
-- If the equipped Value changed, it will get their trail
equippedTrail.Changed:Connect(function()
if equippedTrail.Value ~= nil then
if game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value) then
EquipTrail(player,game.ReplicatedStorage.Trails:FindFirstChild(equippedTrail.Value):Clone())
end
end
if equippedTrail.Value == "" then
local char = player.Character
local root = char:FindFirstChild("HumanoidRootPart")
local head = char:FindFirstChild("Head")
if root:FindFirstChild("Attachment") then root.Attachment:Destroy() end
if head:FindFirstChild("Attachment") then head.Attachment:Destroy() end
if char:FindFirstChild(player.Name.."'s Trail") then char[player.Name.."'s Trail"]:Destroy() end
end
end)
-- loaded up the data
local data
local success, errorMsg = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if data ~= nil then
if data.Coins then
coins.Value = data.Coins
end
if data.EquippedTrail then
equippedTrail.Value = data.EquippedTrail
end
if data.Trails then
for index,trail in pairs(data.Trails) do
local bool = Instance.new("BoolValue",trailInventory); bool.Name = trail
end
end
else
-- New player
print("A new challenger approch")
end
-- This event will fire the client to update the gui
game.ReplicatedStorage.Events.SendData:FireClient(player,data)
end)
game.Players.PlayerRemoving:Connect(function(player)
-- Data holder
local data = {}
data.Coins = player.Coins.Value
data.EquippedTrail = player.EquippedTrail.Value
data.Trails = {}
for i,v in pairs(player.TrailInventory:GetChildren()) do
table.insert(data.Trails,v.Name)
end
local success, errorMsg = pcall(function()
dataStore:SetAsync(player.UserId,data)
end)
-- if its succeed then the data is saved else we got an error
if success then
print("Succesfully saved")
else
print(errorMsg)
end
end)
game:BindToClose(function()
for index, player in pairs(game.Players:GetPlayers()) do
local data = {}
data.Coins = player.Coins.Value
data.EquippedTrail = player.EquippedTrail.Value
data.Trails = {}
for i,v in pairs(player.TrailInventory:GetChildren()) do
table.insert(data.Trails,v.Name)
end
local success, errorMsg = pcall(function()
dataStore:SetAsync(player.UserId,data)
end)
-- if its succeed then the data is saved else we got an error
if success then
print("Succesfully saved")
else
print(errorMsg)
end
end
end)
-- Server Events
game.ReplicatedStorage.Events.BuyItem.OnServerInvoke = function(player, trailName)
local trail = game.ReplicatedStorage.Trails:FindFirstChild(trailName)
local ifsInInventory
if player.TrailInventory:FindFirstChild(trailName) then
ifsInInventory = true
end
if trail then
if trail:FindFirstChild("Price") then
if not ifsInInventory then
-- Check if we can buy the trail
if player.Coins.Value >= trail.Price.Value then
print(player.Name.." brought the ".. trail.Name.." trail")
player.Coins.Value = player.Coins.Value - trail.Price.Value
local bool = Instance.new("BoolValue",player.TrailInventory); bool.Name = trail.Name
return "Brought"
else
return "NotEnough"
end
else
-- You already owned it
if player.EquippedTrail.Value ~= trail.Name then
player.EquippedTrail.Value = trail.Name
print(player.Name.." equipped the ".. trail.Name.." trail")
return "Equip"
else
player.EquippedTrail.Value = ""
print(player.Name.." unequipped the ".. trail.Name.." trail")
return "Unequip"
end
end
end
end
end
if you can find any other errors aswell, please tell me about it. Thanks!

2 comments
Hey, is this a local script or a normal script? I think I may know the problem.
You cant declare localplayer in serverscript