My cooldown zone is not working!

Hello, I am trying to make a zone in which when you enter in, you’ll have a forcefield and when you exit, you’ll no longer have the forcefield and you have to wait 2 minutes to get back the forcefield (basically a sort of safezone with cooldown)
I tried making a table for it but it only focuses to only one player, if the first player enters the zone, then the others that entered can’t have forcefield even if they waited 2 minutes which is not what I want, how can I fix this serverscript?
NOTE: I’m using ZonePlus

local CooldownTable = {}

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.Safezonemodel.Union
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
	if CooldownTable[player.Name] == false or CooldownTable[player.Name] == nil then
		--container.CanCollide = false
		local chr = player.Character
		local ff = Instance.new("ForceField")
		ff.Parent = chr
	end
end)

zone.playerExited:Connect(function(player)
	CooldownTable[player.Name] = true
	--container.CanCollide = true
	local chr = player.Character
	local ff = chr:WaitForChild("ForceField")
	if ff then
		local Debris = game:GetService("Debris")
		Debris:AddItem(ff, 0)
	end
	wait(120)
	CooldownTable[player.Name] = false
end)
1 Like

CooldownTable can’t find player’s name in it, at that point the script should break. also you are trying to set the player’s name (in the table) to true, so you won’t be able to find it again (you can just remove the player’s name from the table after cooldown)

1 Like

Tysm! I’ve tried doing table.remove instead of “CooldownTable[player.name] = false” and it works!

local index = table.find(CooldownTable, player.Name)
table.remove(CooldownTable, index)

Did you change the way values are inserted into the table? If you didn’t then table.find/table.remove wouldn’t work.
table.insert(CooldownTable, player.Name)

well this script works perfectly

local CooldownTable = {}

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.Safezonemodel.Union
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
	if CooldownTable[player.Name] == nil then
		--container.CanCollide = false
		local chr = player.Character
		local ff = Instance.new("ForceField")
		ff.Parent = chr
	end
end)

zone.playerExited:Connect(function(player)
	--CooldownTable[player.Name] = true
	--container.CanCollide = true
	local chr = player.Character
	local ff = chr:WaitForChild("ForceField")
	if ff then
		local Debris = game:GetService("Debris")
		Debris:AddItem(ff, 0)
	end
	wait(120)
	local index = table.find(CooldownTable, player.Name)
	table.remove(CooldownTable, index)
end)

Nothing is being added to the table.

local index = table.find(CooldownTable, player.Name)
table.remove(CooldownTable, index)

This isn’t going to remove anything, you can verify this by printing ‘index’ (‘nil’ will be output).

Oh i’m so stupid, i didn’t verify the cooldown before, but yeah it didn’t work

It’s now fixed! Since the second argument wasn’t a string, it wouldn’t work.
example: table.insert(a, 2)
here, the first argument is an Instance and the second argument is the string.
I had to use the UserId for the second argument then.
Here’s the fixed one:

local CooldownTable = {}

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.Safezonemodel.Union
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
	if table.find(CooldownTable, player.UserId) == nil then
		--container.CanCollide = false
		local chr = player.Character
		local ff = Instance.new("ForceField")
		ff.Parent = chr
	end
end)

zone.playerExited:Connect(function(player)
	--CooldownTable[player.Name] = true
	--container.CanCollide = true
	local chr = player.Character
	local ff = chr:FindFirstChild("ForceField")
	table.insert(CooldownTable, player.UserId)
	print("table inserted")
	if ff then
		local Debris = game:GetService("Debris")
		Debris:AddItem(ff, 0)
	end
	wait(120)
	local index = table.find(CooldownTable, player.UserId)
	table.remove(CooldownTable, index)
	print("table removed")
end)