Hello, i am going to try to keep this simple
-
I want to achieve a “Trails Saving System”
What is a “Trails Saving System” its when a person buys a “Trail” and the server saves it to the dataStore so when you join back you still have it as “Bought” in the shop -
The Saving doesn’t work if one person buys something it saves the bought thing to the server and no the player.
So all the other people also see that it’s bought even though they haven’t. -
I have changed the code a few times searching for solutions online but nothing worked
The Script
local event = game.ReplicatedStorage.Shop
local players = game.Players
local trails = game.ServerStorage.Trails
local DataService = game:GetService("DataStoreService")
local TrailsData = DataService:GetDataStore("TrailsData2")
local OwnedTrails = {}
event.OnServerEvent:Connect(function(plr, TrailName)
local plrID = "player_"..plr.UserId
local char = plr.Character
local head = char.Head
local HRP = char.HumanoidRootPart
local trail = trails:FindFirstChild(TrailName)
local price = trail:FindFirstChild("Price")
local PlrTrails = TrailsData:GetAsync(plrID).OwnedTrails
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats.Coins.Value >= price.Value then
if plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName).Buy.Text ~= "Equipped" then
if not table.find(PlrTrails, TrailName) then
table.insert(PlrTrails, TrailName)
end
if plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName).Buy.Text ~= "Equip" then
leaderstats.Coins.Value -= price.Value
end
plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName).Buy.Text = "Equipped"
task.wait(0.5)
plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName).Buy.Text = "Equip"
if plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName):FindFirstChild("PriceText") then
plr.PlayerGui.Shop.Trails.Trails:FindFirstChild(TrailName).PriceText:Destroy()
end
end
local childHead = head:GetChildren()
if not head:FindFirstChild(TrailName) then
for i, v in pairs(childHead) do
if v:IsA("Trail") then
v:Destroy()
end
end
task.wait(0.1)
plr.Trails.EquippedTrail.Value = TrailName
local EquippedTrail = trail:Clone()
EquippedTrail.Parent = head
EquippedTrail.Attachment0 = head.Attachment0
EquippedTrail.Attachment1 = HRP.Attachment1
end
end
end)
players.PlayerAdded:Connect(function(plr)
local TrailsFolder = Instance.new("Folder", plr)
TrailsFolder.Name = "Trails"
local EquippedTrail = Instance.new("StringValue", TrailsFolder)
EquippedTrail.Name = "EquippedTrail"
local plrID = "player_"..plr.UserId
local data
local success, errormessage = pcall(function()
data = TrailsData:GetAsync(plrID)
end)
if success then
if data then
OwnedTrails = data.OwnedTrails
EquippedTrail.Value = data.EquippedTrail
end
end
plr.CharacterAdded:Connect(function()
local char = plr.Character
local head = char.Head
local HRP = char.HumanoidRootPart
if not head:FindFirstChild("Attachment0") then
local attachment0 = Instance.new("Attachment", head)
attachment0.Name = "Attachment0"
local attachment1 = Instance.new("Attachment", HRP)
attachment1.Name = "Attachment1"
end
local StartingTrail = trails:FindFirstChild(EquippedTrail.Value)
local TrailClone = StartingTrail:Clone()
TrailClone.Parent = head
TrailClone.Attachment0 = head.Attachment0
TrailClone.Attachment1 = HRP.Attachment1
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local plrID = "player_"..plr.UserId
local data = {
OwnedTrails = {};
EquippedTrail = plr.Trails.EquippedTrail.Value;
}
print(data.OwnedTrails)
local success, errormessage = pcall(function()
TrailsData:SetAsync(plrID, data)
end)
if success then
print("TrailsData successfully saved!")
else
print("Error With Saving Trails Data")
warn(errormessage)
end
end)
game.Players.PlayerAdded:Connect(function(plr)
local plrID = "player_"..plr.UserId
repeat wait(1) until plr.PlayerGui:FindFirstChild("Shop")
local TrailsShop = plr.PlayerGui.Shop.Trails.Trails
local TrailsChildren = TrailsShop:GetChildren()
local PlrTrails = TrailsData:GetAsync(plrID).OwnedTrails
print(PlrTrails)
for i, v in pairs(TrailsChildren) do
if v:IsA("Frame") then
if table.find(PlrTrails, v.Name) then
local PriceText = v:FindFirstChild("PriceText")
local Button = v:FindFirstChild("Buy")
Button.Text = "Equip"
PriceText:Destroy()
end
end
end
end)
Thanks for your time. If something is missing or you need more information please ask.