ForeverDev

Data system

Oct 4th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 KB | None | 0 0
  1. local version = Workspace.VERSION.Value
  2.  
  3. local service = game:GetService("DataStoreService"):GetDataStore("ForeverDevStats" .. version)
  4.  
  5. function script.Parent.AwardAchievement.OnInvoke(player, award)
  6.     local frame = game.ReplicatedStorage.Achievement:clone()
  7.     frame.Parent = player.PlayerGui.ScreenGui
  8.     frame.Award.Text = award
  9.     service:SetAsync(player.userId .. award, true)
  10. end
  11.  
  12. function script.Parent.GetData.OnServerInvoke(player, dataName, returnInstance)
  13.     if game.ServerStorage:FindFirstChild(player.Name .. "Data") then
  14.         if returnInstance then
  15.             return game.ServerStorage[player.Name .. "Data"][dataName]
  16.         end
  17.         return game.ServerStorage[player.Name .. "Data"][dataName].Value
  18.     end
  19.     return false
  20. end
  21.  
  22. function script.Parent.ChangeData.OnServerInvoke(player, dataName, value)
  23.     if game.ServerStorage:FindFirstChild(player.Name .. "Data") then
  24.         game.ServerStorage[player.Name .. "Data"][dataName].Value = value
  25.         return game.ServerStorage[player.Name .. "Data"][dataName].Value
  26.     end
  27.     return false
  28. end
  29.  
  30. function script.Parent.AffectPlayer.OnInvoke(player, object)
  31.     print(player.Name)
  32.     if object:IsA("BoolValue") and object.Name:find("ACH") then
  33.         local ach = player.PlayerGui.ScreenGui.Menu.Current.Achievements.Collection[object.Name]
  34.         if object.Value then
  35.             ach.CurrentImage.Value = ach.Images.Earned.Value
  36.         else
  37.             ach.CurrentImage.Value = ach.Images.NotEarned.Value
  38.         end
  39.         game:GetService("PointsService"):AwardPoints(player.userId, 100)
  40.     end
  41. end
  42.  
  43. local version = Workspace.VERSION.Value
  44.  
  45. local storage = game.ServerStorage
  46. local service = game:GetService("DataStoreService"):GetDataStore("ForeverDevStats" .. version)
  47.  
  48. local default = {
  49.     cash = 10,
  50.     ach_thousandcash = false
  51. }
  52.  
  53. local comma = function(number)
  54.     repeat number, count = tostring(number):gsub("^(%d+)(%d%d%d)", "%1,%2") until count == 0
  55.     return number
  56. end
  57.  
  58. game.Players.PlayerAdded:connect(function(player)
  59.     local data;
  60.     local data = storage.Data:clone()
  61.     data.Parent = storage
  62.     data.Name = player.Name .. "Data"
  63.     local screen = player.PlayerGui:WaitForChild("ScreenGui")
  64.     for _, store in ipairs(data:GetChildren()) do
  65.         store.Changed:connect(function(value)
  66.             if store:FindFirstChild("DisplayedOnScreen") then
  67.                 screen[store.Name].Output.Text = screen[store.Name].StringOutput.Value:gsub("VALUE", comma(value))
  68.             end
  69.             if store:FindFirstChild("Achievement") then
  70.                 Workspace.Functions.AffectPlayer:Invoke(player, store)
  71.                 if not service:GetAsync(player.userId .. tostring(store.AchievementKey.Value)) and value then
  72.                     screen.Menu.Current.Achievements.Collection[store.Name].Achieved.Value = true
  73.                     Workspace.Functions.AwardAchievement:Invoke(player, store.AchievementKey.Value)
  74.                 end
  75.             end
  76.         end)
  77.         store.Value = service:GetAsync(tostring(player.userId) .. store.Name:lower()) or default[store.Name:lower()]
  78.     end
  79.    
  80.     player.CharacterAdded:connect(function()
  81.         --eww, incoming disgusting code.  changing a value to the same value doesn't execute the .Changed event, sadly
  82.         coroutine.wrap(function()
  83.             wait(2)
  84.             for _, store in ipairs(data:GetChildren()) do
  85.                 if store:IsA("NumberValue") then
  86.                     store.Value = store.Value + 1
  87.                     store.Value = store.Value - 1
  88.                 elseif store:IsA("BoolValue") then
  89.                     store.Value = not store.Value
  90.                     store.Value = not store.Value
  91.                 end
  92.             end
  93.         end)()
  94.     end)
  95.    
  96. end)
  97.  
  98. game.Players.PlayerRemoving:connect(function(player)
  99.     local data = storage:FindFirstChild(player.Name .. "Data")
  100.     if data then
  101.         for _, store in ipairs(data:GetChildren()) do
  102.             service:SetAsync(tostring(player.userId) .. store.Name:lower(), store.Value)
  103.         end
  104.     end
  105. end)
  106.  
  107. local player = game.Players.LocalPlayer
  108. local screen = script.Parent
  109. local mouse = player:GetMouse()
  110.  
  111. local movements = {}
  112.  
  113. for _, object in ipairs(screen:GetChildren()) do
  114.     if object:IsA("Frame") then
  115.         movements[object.Name] = {pos = object.Position, direction = object.Direction.Value}
  116.     end
  117. end
  118.  
  119. local menu = {
  120.     body = screen:WaitForChild("Menu"),
  121.     current = screen.Menu:WaitForChild("Current"),
  122.     changeView = function(self, mode)
  123.         for _, v in ipairs(self.body.Current:GetChildren()) do
  124.             if v:IsA("Frame") then
  125.                 if v.Name:lower() ~= mode then
  126.                     v:TweenPosition(UDim2.new(0, 0, -1, -1), "Out", "Quad", 1, true)
  127.                 else
  128.                     coroutine.wrap(function()
  129.                         wait(1)
  130.                         v:TweenPosition(UDim2.new(0, 0, 0, 0), "Out", "Quad", 1, true)
  131.                     end)()
  132.                 end
  133.             end
  134.         end
  135.         self.body.Title.Text = mode == "main" and "Menu" or mode:gsub("^.", function(match) return match:upper() end)
  136.     end,
  137.     openOrClose = function()
  138.         local moved;
  139.         local menu = screen.Menu
  140.         if menu.Position == UDim2.new(0.5, -240, 1, 20) then
  141.             moved = "open"
  142.             menu:TweenPosition(UDim2.new(0.5, -240, 0.5, -200), "Out", "Back", 1)
  143.             coroutine.wrap(function()
  144.                 wait(2)
  145.                 Workspace.Functions.ChangeData:InvokeServer("ACH_MenuExplorer", true)
  146.             end)()
  147.         elseif menu.Position == UDim2.new(0.5, -240, 0.5, -200) then
  148.             moved = "close"
  149.             menu:TweenPosition(UDim2.new(0.5, -240, 1, 20), "Out", "Quad", 1)
  150.         end
  151.         if moved then
  152.             for _, object in ipairs(screen:GetChildren()) do
  153.                 if object:IsA("Frame") and object.Name ~= "Achievement" and object ~= menu then
  154.                     object:TweenPosition(movements[object.Name].pos + UDim2.new(0, moved == "open" and (movements[object.Name].direction == "left" and -object.Size.X.Offset or object.Size.X.Offset) or movements[object.Name].pos.X.Offset, 0, 0), "Out", "Quad", 0.9, true)
  155.                 end
  156.             end
  157.         end
  158.     end
  159. }
  160.  
  161. local hoverEvents = function(object, leave, enter, down)
  162.     local save = object.Position
  163.     object.MouseEnter:connect(function()
  164.         object.Image = "rbxassetid://" .. enter
  165.         object:TweenPosition(save - UDim2.new(0, 0, 0, 5), "Out", "Quad", 0.5, true)
  166.     end)
  167.     object.MouseLeave:connect(function()
  168.         object.Image = "rbxassetid://" .. leave
  169.         object:TweenPosition(save, "Out", "Bounce", 0.5, true)
  170.     end)
  171.     object.MouseButton1Down:connect(function()
  172.         object.Image = "rbxassetid://" .. down
  173.     end)
  174.     object.MouseButton1Up:connect(function()
  175.         object.Image = "rbxassetid://" .. leave
  176.         object:TweenPosition(save, "Out", "Quad", 0.5, true)
  177.     end)
  178. end
  179.  
  180. local newClickEvent = function(class, object, ...)
  181.     local args = {...}
  182.     if class == "close" then
  183.         object.MouseButton1Click:connect(function()
  184.             menu:openOrClose()
  185.             menu:changeView("main")
  186.         end)
  187.     elseif class == "view" then
  188.         object.MouseButton1Click:connect(function()
  189.             menu:changeView(args[1])
  190.         end)
  191.     end
  192. end
  193.  
  194. mouse.KeyDown:connect(function(key)
  195.     if key:lower() == "p" then
  196.         menu:openOrClose()
  197.     end
  198. end)
  199.  
  200. hoverEvents(menu.current.Main.Achievements, "180893368", "180737861", "180737435")
  201. menu.current.Main.Achievements.MouseButton1Click:connect(function()
  202.     menu:changeView("achievements")
  203. end)
  204.  
  205. hoverEvents(menu.current.Achievements.Close, "180818606", "180823928", "180824362")
  206. newClickEvent("close", menu.current.Achievements.Close)
  207.  
  208. hoverEvents(menu.current.Achievements.Back, "180826525", "180827456", "180827481")
  209. newClickEvent("view", menu.current.Achievements.Back, "main")
  210.  
  211.  
  212. local pos = {}
  213. repeat wait() until #script.Parent:GetChildren() >= 3
  214.  
  215. for _, label in ipairs(script.Parent:GetChildren()) do
  216.     if label:IsA("ImageLabel") then
  217.         label.Image = "rbxassetid://" .. (Workspace.Functions.GetData:InvokeServer(label.Name) and label.Images.Earned.Value or label.Images.NotEarned.Value)
  218.         label:WaitForChild("CurrentImage").Changed:connect(function(value)
  219.             label.Image = "rbxassetid://" .. label.Images.Earned.Value
  220.         end)
  221.         pos[label.Name] = {position = label.Position, size = label.Size}
  222.         label.MouseEnter:connect(function()
  223.             label:TweenSizeAndPosition(pos[label.Name].size + UDim2.new(0, 4, 0, 4), pos[label.Name].position - UDim2.new(0, 2, 0, 2), "Out", "Linear", 0.2, true)
  224.             script.Parent.Parent:WaitForChild("Display").Text = (Workspace.Functions.GetData:InvokeServer(label.Name) and "| ACHIEVED | " or "| NOT ACHIEVED | ") .. "\n" .. label.Display.Value:gsub("LINE", "\n")
  225.         end)
  226.         label.MouseLeave:connect(function()
  227.             label:TweenSizeAndPosition(pos[label.Name].size, pos[label.Name].position, "Out", "Linear", 0.2, true)
  228.             script.Parent.Parent:WaitForChild("Display").Text = ""
  229.         end)
  230.     end
  231. end
Advertisement
Add Comment
Please, Sign In to add comment