I’m trying to handle player’s leaderstats by using Module Scripts.
Every player will be granted a Data Module script upon joining the game, it’s stored inside ServerStorage such that player/exploiter can not access and edit it. currently it handles 3 data: Eliminations, Deaths and Streaks. It’s also referenced by game.Players.Player.leaderstats to use the in-game leaderboard stats.
Whenever the game detects a player gets an elimination, it will use a RemoteEvent to request the server to add an elimination, streak to the eliminator and add a death to the eliminated player. The server will require the eliminator’s player Module script upon OnServerEvent, and add the corresponding data to the eliminator and eliminated.
A client will send a table to the server, including the DataName, Type (Including Add, Minus, Reset), Amount, TargetPlayer (Player to modify stats to)
Client script: (This will be simplified into a function)
-- Add 1 elimination and 1 streak to eliminator
RemoteEvents.UpdateLeaderstats:FireServer( {DataName = "Eliminations", Type = "Add", Amount = 1, TargetPlayer = plr.Name} )
RemoteEvents.UpdateLeaderstats:FireServer( {DataName = "Streak", Type = "Add", Amount = 1, TargetPlayer = plr.Name} )
-- Reset eliminated streak and Add 1 Death
RemoteEvents.UpdateLeaderstats:FireServer( {DataName = "Deaths", Type = "Add", Amount = 1, TargetPlayer = HitPlr.Name} )
RemoteEvents.UpdateLeaderstats:FireServer( {DataName = "Streak", Type = "Reset", Amount = 1, TargetPlayer = HitPlr.Name} )
Server Script:
RemoteEvents.UpdateLeaderstats.OnServerEvent:Connect(function(FireFrom, DataTable)
print(DataTable.TargetPlayer)
-- Data is named "DATA_" PlayerName
local MS = require(ss.Data:FindFirstChild("DATA_"..DataTable.TargetPlayer))
MS.UpdateLeaderstats(FireFrom,DataTable)
end)
Module Script:
function DataModule.UpdateLeaderstats(RequestFrom,DataTable)
local TargetPlayer = DataTable.TargetPlayer
local DataName = DataTable.DataName
local Type = DataTable.Type
print(RequestFrom," requests to change ", TargetPlayer,"stats: ", DataName ," by ", Type, DataTable)
-- This will be simplified to function
if Type == "Add" then
game.Players:FindFirstChild(TargetPlayer).leaderstats:FindFirstChild(DataName).Value = game.Players:FindFirstChild(TargetPlayer).leaderstats:FindFirstChild(DataName).Value + 1
elseif Type == "Minus" then
game.Players:FindFirstChild(TargetPlayer).leaderstats:FindFirstChild(DataName).Value = game.Players:FindFirstChild(TargetPlayer).leaderstats:FindFirstChild(DataName).Value - 1
elseif Type == "Reset" then
game.Players:FindFirstChild(TargetPlayer).leaderstats:FindFirstChild(DataName).Value = 0
end
end
Despite on the code keep repeating itself, overall, In terms of game security, efficiency, is it a good method or way to handle stats? Is this way better than simply adding stats by Value = Value + amount by simply requesting from the client?