I’m currently working on a clashing system where players need to press the corresponding keys, and the one who presses the most keys that appear on the screen wins. The problem is that the table storing the ongoing clashes is being returned empty by the server. I really don’t know what could be causing this.
Module Script:
--// Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
--// Constants
local Events = ReplicatedStorage:FindFirstChild("Events")
--// Functions
local ClashModule = {}
local ActiveClashes = {}
ClashModule.Keys = {
"Z", "X", "C", "B", "N", "V", "R", "T", "K", "L", "Z", "E","F"
}
local function GenerateId()
return "Clash_" .. HttpService:GenerateGUID(false)
end
local function ShuffleKeys()
local keys = table.clone(ClashModule.Keys)
for i = #keys, 2, -1 do
local j = math.random(1, i)
keys[i], keys[j] = keys[j], keys[i]
end
return keys
end
function ClashModule:StartClash(Character_1 : Model, Character_2 : Model, Duration : number)
local ClashId = GenerateId()
local KeyOrder = ShuffleKeys()
ActiveClashes[ClashId] = {
[Character_1] = {Score = 0, lastPress = 0, index = 1};
[Character_2] = {Score = 0, lastPress = 0, index = 1};
KeyOrder = KeyOrder
}
if not Players:GetPlayerFromCharacter(Character_1) then
ActiveClashes[ClashId][Character_1].Score = math.random(5,12)
else
local Player_1 = Players:GetPlayerFromCharacter(Character_1)
Events:FindFirstChild("Clash"):FireClient(Player_1, "ClashStarted", ClashId, KeyOrder)
end
if not Players:GetPlayerFromCharacter(Character_2) then
ActiveClashes[ClashId][Character_2].Score = math.random(5,12)
else
local Player_2 = Players:GetPlayerFromCharacter(Character_2)
Events:FindFirstChild("Clash"):FireClient(Player_2, "ClashStarted", ClashId, KeyOrder)
end
task.wait(Duration)
--[[local p1Score = ActiveClashes[ClashId][Character_1].Score
local p2Score = ActiveClashes[ClashId][Character_2].Score
local Winner = p1Score > p2Score and Character_1 or Character_2
if p1Score == p2Score then
local random = math.random(1,2)
if random == 1 then
Winner = Character_1
else
Winner = Character_2
end
end
ActiveClashes[ClashId] = nil
print(Winner)]]
end
function ClashModule:RegisterInput(Character : Model, ClashId)
if ActiveClashes[ClashId] then
local ActiveClashesTable = ActiveClashes[ClashId]
if ActiveClashesTable[Character] then
ActiveClashesTable[Character].Score = ActiveClashesTable[Character] + 1
print("New Score for :", Character.Name)
end
end
end
function ClashModule:GetActiveClash(ClashId)
--->> table is empty when i call this function
if ActiveClashes[ClashId] then
return ActiveClashes[ClashId]
end
end
return ClashModule
Server
Events:FindFirstChild("Clash").OnServerEvent:Connect(function(Player : Player, FunctionName : string, ...)
local Character = Player.Character
local Parameters = {...}
if FunctionName == "RegisterKey" then
local Input = Parameters[1]
local ClashId = Parameters[2]
print(ClashModule:GetActiveClash(ClashId)) -->> output: {}
if ClashModule:GetActiveClash(ClashId) then
local CurrentClashTable = ClashModule:GetActiveClash(ClashId)
end
end
end)