When one of the functions is fired it will reset both cooldowns for both functions
Like if ability [F] is ready to be used since it has an 8-second cooldown period I would expect ability [E] to be ready to be used as well but it won’t after [F] is used.
I would assume it is acting this way because of: playertable[player.Name] = tick()
How would I make them work separately?
–Local script
local userInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Ability = ReplicatedStorage:WaitForChild("Ability")
function Press (input, istyping)
if istyping then return end
if input.KeyCode == Enum.KeyCode.E then
Ability:FireServer("E")
elseif input.KeyCode == Enum.KeyCode.F then
Ability:FireServer("F")
end
end
userInputService.InputBegan:Connect(Press)
– Server script
local useAbility = game:GetService("ReplicatedStorage"):WaitForChild("Ability")
---
local playertable = {}
game.Players.PlayerAdded:Connect(function(player)
if not playertable[player.Name] then
playertable[player.Name] = tick()
end
end)
game.Players.PlayerRemoving:Connect(function(player)
if playertable[player.Name] then
playertable[player.Name] = nil
end
end)
---
local cooldownE = 5
local cooldownF = 8
---
useAbility.OnServerEvent:Connect(function(player, button)
if button == "E" then
ability1(player)
elseif button == "F" then
ability2(player)
end
end)
---
function ability1(player)
if tick() - playertable[player.Name] >= cooldownE then
playertable[player.Name] = tick()
print("used first abiliy")
end
end
function ability2(player)
if tick() - playertable[player.Name] >= cooldownF then
playertable[player.Name] = tick()
print("used second abiliy")
end
end
You’re referencing the same value in the same table, for each function. I’d recommend what bits said and just make two separate tables to handle each of these cooldowns.
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local Ability = RS:WaitForChild("Ability")
local function press(input, processed)
if processed then return end
if input.KeyCode == Enum.KeyCode.E then
Ability:FireServer("E")
elseif input.KeyCode == Enum.KeyCode.F then
Ability:FireServer("F")
end
end
UIS.InputBegan:Connect(press)
local RS = game:GetService("ReplicatedStorage")
local Plrs = game:GetService("Players")
local Ability = RS:WaitForChild("Ability")
---
local eAbilityPlayers = {}
local fAbilityPlayers = {}
local cooldownE = 5
local cooldownF = 8
---
Plrs.PlayerAdded:Connect(function(player)
if not eAbilityPlayers[player.Name] then
eAbilityPlayers[player.Name] = tick()
end
if not fAbilityPlayers[player.Name] then
fAbilityPlayers[player.Name] = tick()
end
end)
Plrs.PlayerRemoving:Connect(function(player)
if eAbilityPlayers[player.Name] then
eAbilityPlayers[player.Name] = nil
end
if fAbilityPlayers[player.Name] then
fAbilityPlayers[player.Name] = nil
end
end)
---
local function ability1(player)
if tick() - eAbilityPlayers[player.Name] >= cooldownE then
eAbilityPlayers[player.Name] = tick()
print("used first abiliy")
end
end
local function ability2(player)
if tick() - fAbilityPlayers[player.Name] >= cooldownF then
fAbilityPlayers[player.Name] = tick()
print("used second abiliy")
end
end
---
Ability.OnServerEvent:Connect(function(player, button)
if button == "E" then
ability1(player)
elseif button == "F" then
ability2(player)
end
end)
and the server script. You just needed two different arrays of player objects corresponding to ability use times for each ability.