please can someone help me fix this script so that it doesn’t create a new folder and uses a folder i have already created
local StatsName = “Gems” – Enter your Stats Name (Current is “Cash”)
local StartingAmt = 10000 – 10,000 Cash on Playing Game
local DataStoreService = game:GetService(“DataStoreService”)
local RunService = game:GetService(“RunService”)
local PlayersService = game:GetService(“Players”)
local statsStore = DataStoreService:GetDataStore(“-stats”)
game.Players.PlayerAdded:Connect(function(Player)
local leaderFolder = Instance.new(“Folder”)
leaderFolder.Name = “leaderstats”
leaderFolder.Parent = Player
local statsValue = Instance.new("NumberValue")
statsValue.Value = StartingAmt
statsValue.Name = StatsName
statsValue.Parent = leaderFolder
local lastEquipped = Instance.new("StringValue")
lastEquipped.Value = "nil"
lastEquipped.Parent = Player
lastEquipped.Name = "equippedTrail"
local oldData
local foundData,newplr = pcall(function()
oldData = statsStore:GetAsync(Player.UserId)
end)
if foundData and oldData ~= nil then
statsValue.Value = oldData.Stat
lastEquipped.Value = oldData.equip
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local succesSaved,ErrorSaved = pcall(function()
statsStore:SetAsync(Player.UserId,{
Stat = Player.leaderstats:FindFirstChild(StatsName).Value,
equip = Player.equippedTrail.Value,
})
end)
end)
game:BindToClose(function()
if RunService:IsStudio() then
wait(1)
else
for _, Player in pairs(PlayersService) do
local succesSaved,ErrorSaved = pcall(function()
statsStore:SetAsync(Player.UserId,{
Stat = Player.leaderstats:FindFirstChild(StatsName).Value,
equip = Player.equippedTrail.Value
})
end)
end
end
end)
----- // The Below Part is Used for Trail Remove Events
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local REEvent = ReplicatedStorage:FindFirstChild(“trailShopRE”)
REEvent.OnServerEvent:Connect(function(Player,TrailName,TrailColour,state)
if state == “equip” and Player.Character then
local success,errormsg = pcall(function()
local HRP = Player.Character.HumanoidRootPart
for i, oldTrail in pairs(HRP:GetChildren()) do
if oldTrail:IsA('Trail') or oldTrail.Name == "trailAttachment" then
oldTrail:Destroy()
end
end
for i, oldTrail in pairs(Player.Character.Head:GetChildren()) do
if oldTrail:IsA('Trail') or oldTrail.Name == "trailAttachment" then
oldTrail:Destroy()
end
end
-- local r, g, b = R, G, B
-- local strOne = string.format("%0.f", r*255)
-- local strTwo = string.format("%0.f", g*255)
-- local strThree = string.format("%0.f", b*255)
-- print(strOne .. " " .. strTwo .. " " .. strThree)
-- local Finalcolour = Color3.new(strOne,strTwo,strThree)
local newTrail = Instance.new("Trail")
newTrail.Name = TrailName
newTrail.Color = TrailColour
newTrail.Parent = HRP
newTrail.MaxLength = 10
local attachmentOne = Instance.new("Attachment")
attachmentOne.Name = "trailAttachment"
attachmentOne.Parent = HRP
local attachmentTwo = Instance.new("Attachment")
attachmentTwo.Name = "trailAttachment"
attachmentTwo.Parent = Player.Character.Head
newTrail.Attachment0 = attachmentOne
newTrail.Attachment1 = attachmentTwo
Player.equippedTrail.Value = TrailName
end)
if not success then
warn(errormsg)
end
end
end)
REEvent.OnServerEvent:Connect(function(Player,TrailName,state)
if state == ‘unequip’ then
if Player.Character then
local unequipped,stillEquipped = pcall(function()
local HRP = Player.Character.HumanoidRootPart
for i, oldTrail in pairs(HRP:GetChildren()) do
if oldTrail:IsA('Trail') or oldTrail.Name == "trailAttachment" then
oldTrail:Destroy()
end
end
for i, oldTrail in pairs(Player.Character.Head:GetChildren()) do
if oldTrail:IsA('Trail') or oldTrail.Name == "trailAttachment" then
oldTrail:Destroy()
end
end
Player.equippedTrail.Value = "nil"
end)
if not unequipped then
warn(stillEquipped)
end
end
end
end)
REEvent.OnServerEvent:Connect(function(Player,Price,TrailName,state)
if state == “purchase” then
local check,errored = pcall(function()
if Player.leaderstats:FindFirstChild(StatsName).Value >= Price then
Player.leaderstats:FindFirstChild(StatsName).Value -= Price
REEvent:FireClient(Player,“purchaseSuccess”,TrailName)
local newValue = Instance.new("BoolValue")
newValue.Name = TrailName
newValue.Value = true
newValue.Parent = Player.trailsOwned
end
end)
if not check then
warn(errored)
end
end
end)