· 5 years ago · Apr 28, 2020, 07:46 PM
1--[[ Last Updated 04/28/2020;
2]]--
3
4local ReplicatedStorage = game:GetService("ReplicatedStorage")
5local DataStoreService = game:GetService("DataStoreService")
6local CodesFunction = ReplicatedStorage:WaitForChild("CodesFunction")
7local DataStore = DataStoreService:GetDataStore("CodesData") -- Gets the data store
8local Codes = require(script.Codes) -- Requires the module Codes which is inside the CodesScript
9
10game.Players.PlayerAdded:Connect(function(Player) -- On PlayerAdded...
11 local CodesFolder = Instance.new("Folder") -- Creates the CodesFolder
12 CodesFolder.Name = "CodesFolder"
13 CodesFolder.Parent = Player
14
15 local Data = DataStore:GetAsync(Player.UserId) -- Gets the stored data
16 if Data then -- Checks if theres data saved
17 for i, v in pairs(Data) do -- For each data it will create a BoolValue and store it in CodesFolder
18 local BoolValue = Instance.new("BoolValue")
19 BoolValue.Name = v
20 BoolValue.Parent = CodesFolder
21 end
22 end
23end)
24
25game.Players.PlayerRemoving:Connect(function(Player) -- On PlayerRemoving
26 local Data = {} -- Inside this table we are going to store all the codes that the player has redeemed and then save the table in the datastore
27
28 for i, v in pairs(Player.CodesFolder:GetChildren()) do -- For each childern of the codesfolder... inserts a key inside the table Data
29 table.insert(Data, v.Name)
30 end
31
32 DataStore:SetAsync(Player.UserId, Data) -- Saves data
33end)
34
35function CodesFunction.OnServerInvoke(Player, Code) -- Function invoked when the client invokes the server with the RemoteFunction CodesFunction
36 local CodesFolder = Player:WaitForChild("CodesFolder") -- The folder that we will use to save the codes that the player has redeemed
37
38 if Codes[Code] and not CodesFolder:FindFirstChild(Code) then -- If the code exists inside the module and if the player has not already redeemed the code then...
39 local CodeTable = Codes[Code] -- We get the code from the table inside the module
40 local CodeActive = CodeTable[1] -- We get the first array inside the table which in this case is the enabled
41 local CodeReward = CodeTable[2] -- We get the second array inside the table which in this case is the reward
42
43 if CodeActive then -- if the code is active then...
44 local BoolValue = Instance.new("BoolValue") -- Creates and stores a BoolValue inside the codesfolder so then we can know which codes the player has redeemed
45 BoolValue.Name = Code
46 BoolValue.Parent = CodesFolder
47
48 Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + CodeReward -- Adds the reward to the player's leaderstats, in this tutorial is the Money
49
50 return 3
51 else
52 return 2
53 end
54 else
55 return 1
56 end
57end