Referencing a value through a variable in a table makes me unable to change it

I was making a quick and simple server sided cooldown module script for player moves and such. I store the actions the player can do in a table as the index, and the last time it was used as the value.
Upon receiving the remote event I try to reference the last time the action was used through a variable and change the value but it doesnt seem to change. However if i just get the value from the table without using a variable it works perfectly fine. I was quite confused by this so instead of just ignoring it and going on i wanted to know why this happens. Here is the code.

local PlayerCooldowns = {
	Cooldowns = {
		M1 = 1,
		Critical = 5,
		Counter = 15
	}
	
}


local Players = game:GetService("Players")

for _ , Player  in ipairs(Players:GetChildren()) do
	PlayerCooldowns.Player = { 
		M1 = 0,
		Critical = 0,
		Counter = 0
	}
end

Players.PlayerAdded:Connect(function(Player)
	if PlayerCooldowns.Player then return end
	
	PlayerCooldowns.Player = { 
		M1 = 0,
		Critical = 0,
		Counter = 0
	}
	
end)

local SendInput = game:GetService("ReplicatedStorage"):WaitForChild("SendInput")

SendInput.OnServerEvent:Connect(function(Player, action)
	
	
	local LastTimeActionUsed = PlayerCooldowns.Player[action]
	local ActionCooldown = PlayerCooldowns.Cooldowns[action]
	
	if LastTimeActionUsed ~= nil and ActionCooldown ~= nil then
		local now = tick()
		if now - LastTimeActionUsed > ActionCooldown then
			print("move has been off cooldown for: ".. now - LastTimeActionUsed)
			PlayerCooldowns.Player[action] = now -- if i change PlayerCooldowns.Player[action] to LastTimeActionUsed then the value wont change
		else
			print("move on cooldown")
		end
		
		
	end
	
	
	
end)


return PlayerCooldowns

Its probably something simple i missed but if someone can explain it that would be great

1 Like

I believe you intended to use

PlayerCooldowns[Player]

Rather than .Player

1 Like

It is actually a simple problem.

You are making a variable which stores the value of the action, not the pathway to the value.
Consider the following code:

local TABLE = {"string"}
local value = TABLE[1] -- this store string, NOT the table.
print(value)
value = "LOL" -- this makes the value variable to store a new string, NOT changing the value of the table.
1 Like

That has nothing to do with the OP’s issue. PlayerCooldowns[Player] is equivalent to PlayerCooldowns.Player.

That’s not true.

PlayerCooldowns.Player indexes a key called “Player” from PlayerCooldowns

PlayerCooldowns[Player] indexes a key that is the player userdata

Example:

local t = {}
local key = "abc"
t.key = 1
key = "def"
t.key = 2

print(t)
Result:
{
  ["key"] = 2
}
local t = {}
local key = "abc"
t[key] = 1
key = "def"
t[key] = 2

print(t)
Result:
{
  ["abc"] = 1,
  ["def"] = 2
}

Edit: made my comment a little more polite.

3 Likes

That makes a lot of sense… thanks for that

this is true but i did use the .Player throughout the entire script so it wasnt the issue. Still useful information either way so thanks for that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.