Hello! I just made a functional reward system which I think is pretty efficient but I want to know what the community thinks.
-- services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
-- constants
local CONFIG = ServerScriptService.Config
local PLAYER_DATA = ReplicatedStorage.PlayerData
local REWARD = script.Reward
-- thinking of making some of these indexes into dictionarys for better understanding from peers!
local REWARD_DATA = {
OnAction = {
Kill = 25 ,
Base = 50 ,
Hack = 100,
ZOD = 25 ,
},
OnQueue = {
ZOD = {50,60} ,
Void = {10,10} ,
Cap = {2,10} ,
Play = {500,900},
},
}
local REWARD_TIME = {} -- to manage the players reward functions. So when they leave we can clean up them so they dont memory leak
-- functions
local function UpdateCash(playerData,actionName,actionAccess)
while CONFIG[actionName].Value == true do
wait(REWARD_DATA.OnQueue[actionName][2])
if actionAccess == "All" or Players:FindFirstChild(playerData.Name).Team.Name == actionAccess then
playerData.Currency.ZCASH.Value += REWARD_DATA.OnQueue[actionName][1]
end
end
end
local function RewardPlayer(player,...)
local config = {...}
local action_Type = config[1][1]
local action_Value = config[1][2]
local action_Access = config[1][3]
local PlayerData = PLAYER_DATA:FindFirstChild(player.Name) or PLAYER_DATA:WaitForChild(player.Name)
if action_Type and action_Value then
if action_Type == "OnQueue" then
REWARD_TIME[player.UserId] = {}
REWARD_TIME[player.UserId][action_Value .. player.UserId] = coroutine.wrap(UpdateCash)(PlayerData,action_Value,action_Access,player.UserId)
else
if PlayerData:FindFirstChild("Currency") then
PlayerData.Currency.ZCASH.Value += REWARD_DATA.OnQueue[action_Value][1]
end
end
else
return false
end
end
game:BindToClose(function()
table.clear(REWARD_TIME)
end)
Players.PlayerRemoving:Connect(function(player)
table.clear(REWARD_TIME[player.UserId])
end)
REWARD.OnInvoke = RewardPlayer