HowToRoblox

TrailApplier

Jun 9th, 2021 (edited)
1,626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("DATA")
  3.  
  4. local trails = game.ReplicatedStorage:WaitForChild("Trails")
  5.  
  6.  
  7. function createAtchs(char)
  8.    
  9.     local hrp = char:WaitForChild("HumanoidRootPart")
  10.  
  11.     local atchTop = Instance.new("Attachment", hrp)
  12.     atchTop.Name = "TrailTop"
  13.     atchTop.Position = Vector3.new(0, 0.766, 0)
  14.  
  15.     local atchBtm = Instance.new("Attachment", hrp)
  16.     atchBtm.Name = "TrailBottom"
  17.     atchBtm.Position = Vector3.new(0, -0.766, 0)
  18. end
  19.  
  20.  
  21. function saveData(plrLeaving)
  22.    
  23.     local ownedTrails = {}
  24.     for i, trail in pairs(plrLeaving.OwnedTrails:GetChildren()) do
  25.         table.insert(ownedTrails, trail.Name)
  26.     end
  27.  
  28.     local success, err = pcall(function()
  29.         ds:SetAsync("trails-" .. plrLeaving.UserId, ownedTrails)
  30.         ds:SetAsync("money-" .. plrLeaving.UserId, plrLeaving.leaderstats.Money.Value)
  31.     end)
  32. end
  33.  
  34.  
  35. game.Players.PlayerAdded:Connect(function(plr)
  36.    
  37.    
  38.     local trailsOwned = {}
  39.     local money = 2000
  40.     pcall(function()
  41.         trailsOwned = ds:GetAsync("trails-" .. plr.UserId) or {}
  42.         money = ds:GetAsync("money-" .. plr.UserId) or 2000
  43.     end)
  44.    
  45.    
  46.     local ls = Instance.new("Folder", plr)
  47.     ls.Name = "leaderstats"
  48.    
  49.     local moneyValue = Instance.new("IntValue", ls)
  50.     moneyValue.Name = "Money"
  51.     moneyValue.Value = money
  52.    
  53.    
  54.     local ownedFolder = Instance.new("Folder", plr)
  55.     ownedFolder.Name = "OwnedTrails"
  56.    
  57.    
  58.     for i, owned in pairs(trailsOwned) do
  59.        
  60.         if trails:FindFirstChild(owned) then
  61.            
  62.             trails[owned]:Clone().Parent = ownedFolder
  63.         end
  64.     end
  65.    
  66.     if plr.Character then createAtchs(plr.Character) end
  67.    
  68.     plr.CharacterAdded:Connect(createAtchs)
  69. end)
  70.  
  71.  
  72. game.Players.PlayerRemoving:Connect(saveData)
  73.  
  74. game:BindToClose(function()
  75.    
  76.     for i, plrLeaving in pairs(game.Players:GetPlayers()) do
  77.         saveData(plrLeaving)
  78.     end
  79. end)
  80.  
  81.  
  82. game.ReplicatedStorage.TrailSelectedRE.OnServerEvent:Connect(function(plr, buying, trail)
  83.    
  84.     if buying and not plr.OwnedTrails:FindFirstChild(trail.Name) then
  85.        
  86.         local price = trail.Price.Value
  87.         local money = plr.leaderstats.Money
  88.        
  89.         if price <= money.Value then
  90.            
  91.             money.Value -= price
  92.            
  93.            
  94.             trail:Clone().Parent = plr.OwnedTrails
  95.         end
  96.        
  97.        
  98.     elseif not buying and plr.OwnedTrails:FindFirstChild(trail.Name) then
  99.        
  100.         local char = plr.Character
  101.        
  102.         if not char or not char:FindFirstChild("HumanoidRootPart") then return end
  103.        
  104.         for i, child in pairs(char.HumanoidRootPart:GetChildren()) do
  105.            
  106.            
  107.             if child:IsA("Trail") then child:Destroy() end
  108.         end
  109.        
  110.        
  111.         local newTrail = trail:Clone()
  112.  
  113.         newTrail.Attachment0 = char.HumanoidRootPart.TrailTop
  114.         newTrail.Attachment1 = char.HumanoidRootPart.TrailBottom
  115.  
  116.         newTrail.Parent = char.HumanoidRootPart
  117.     end
  118. end)
Add Comment
Please, Sign In to add comment