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