Reward System review

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
4 Likes

I know this is a late response, but your code seems very efficient. You organized it very well, made it easy to read, and I don’t see any major issues. You mentioned in your comments that you are considering making the OnQueue tables into dictionaries instead. If you know what you’re doing, it isn’t NEEDED, but just for the fun of it you can. Let’s say someone else is adding things to the script (and you don’t think they can handle it) then maybe changing it to a dictionary isn’t a bad idea, but I don’t see an issue with it.

You did a really good job!

From someone who is still a baby scripter: This looks VERY clean. I like it a lot. Really appreciate it when people organize their scripts like this!