Storing player data in game using a module script implementing for all player

Hey so I am having a problem pretty sure dev forum users can solve it
What is the player data I use

Note: I am not storing these data in the database I just need it in the game

so basically I use module scripts to put player data(like cooldown and debounce)and for some reason when I change the debounce in player1 it changes to player1 and player2
Module script

local Data = {}

local DataTemplate = {
	["PurpleOrb"] = {
		Debounce = 0
	},
	
	
	
}

local PlayerData = {}

function Data.AddPlayer(Player) 
	PlayerData[Player] = DataTemplate

end

function Data.ReturnData(Player,PathIndex) 
	print(PlayerData)
	return PathIndex ~= nil and PlayerData[Player][PathIndex] or PlayerData[Player] or PlayerData
end



function Data.RemovePlayer(Player) 
	PlayerData[Player] = nil
end






return Data

Script where I access these data

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScript = game:GetService("ServerScriptService")
--[[Directories]]
local Remotes = ReplicatedStorage:WaitForChild("Remote")
--[[Import]]
local DataManagement = require(game.ReplicatedStorage.DataManager)
--[[Remote]]
local ActivateRemote = Remotes:WaitForChild("ActivateRemote")
local CooldownStart =  Remotes.CooldownStart

--[[Variables]]

--[[Objects]]

--[[Functions]]

--[[Application]]

ActivateRemote.OnServerEvent:Connect(function(Player,ExtraData)
	
	local PlayerData = DataManagement.ReturnData(Player.Name,"PurpleOrb")
	
	
	if   (os.clock() - PlayerData.Debounce ) < 5 then		print(PlayerData.Debounce) return end

	PlayerData.Debounce = os.clock()
	print(PlayerData.Debounce,"Passed",Player.Name)


		
	

end)

Here is the DataCore server script

--Connected to player replicated storage 



--[[Services]]
local PhysicsService = game:GetService("PhysicsService")
--[[Directories]]
local Remote = game.ReplicatedStorage.Remote
--[[Import]]
local DataModule = require(game.ReplicatedStorage.DataManager)
--[[Remote]]
local RequestPlayerDataRemote = Remote.RequestPlayerData
--[[Variables]]

--[[Objects]]
local Players = game:GetService("Players")

--[[Functions]]
--'
--PhysicsService:CreateCollisionGroup("PlayersGroup")


--function OnCharacterAdded(character)
	
--	for _,part in pairs(character:GetDescendants()) do
--		if part:IsA("BasePart") then
--			 PhysicsService:SetPartCollisionGroup(part,"PlayersGroup")
--	   	end
--	end	
--end'
function PlayerJoined(Player)
	DataModule.AddPlayer(Player.Name) 
	
	
	--Player.CharacterAdded:Connect(OnCharacterAdded)
	
	
end

function PlayerLeft(Player)
	DataModule.RemovePlayer(Player) 
end

function RetrieveData(Player)
	RequestPlayerDataRemote:FireClient(Player, DataModule.ReturnData(Player)) 
end


--[[Application]]

Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerRemoving:Connect(PlayerLeft)
RequestPlayerDataRemote.OnServerEvent:Connect(RetrieveData)

Here is another code that when a player clicks while using the tool it fires a remote

--Teleportation

--[[Services]]
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScript = game:GetService("ServerScriptService")
local UIS = game:GetService("UserInputService")
--[[Import]]

--[[Remote]]
local BlackHoleActivate = ReplicatedStorage:WaitForChild("Remote"):WaitForChild("ActivateRemote")
--[[Variables]]
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
--[[Objects]]
local Tool = script.Parent

--[[Functions]]

--[[Application]]


Tool.Activated:Connect(function()
	
	local MousePosition = Mouse.Hit.Position

	BlackHoleActivate:FireServer({Ability = "PurpleOrb"})
	
end)


there are no errors

here is how I put these scripts

I will review everything now

What I want to achieve
Making data set to specific players not all players
What is the problem
It is set to all players, not the player I want

Best wishes.

1 Like

Tables don’t copy by reference, so they’re all sharing the same table. You’ll need to copy the table manually, or move the DataTemplate to the inside of the function

2 Likes

Thanks. It works well now. I have been searching for a solution for long time so thank you a lot

2 Likes