Script Not Detecting Table Value Changes

Hey. I was making a script that would check if a player is still on the hill while a stopwatch was counting how long they were on the hill. The problem is that the script doesn’t detect when the table’s values changes. Any help is appreciated, thanks :smiley:

local hill = game.Workspace.Hill
local status = hill.Status
local holder = hill.Holder

local playersOnHill = {}

local function counter(player, intVal, Table)
	print(Table)
	local count = 0
	while count < 5 do
		local function getUpdatedTable(tab)
			if table.find(tab, tostring(player.Name)) then
				return true
			end
		end
		wait(1)
		count = count + 1
		intVal.Value = count
		local isPlayerOnHill = getUpdatedTable(playersOnHill)
		if isPlayerOnHill == true then
			intVal:Destroy()
			break
		end
		if count == 5 then
			break
		end
	end
	status.Value = "Held by: ".. player.Name
	holder.Value = player.Name
	intVal:Destroy()
end

while wait(1) do
	local touchingParts = GetTouchingParts(hill)
	for i,v in pairs(touchingParts) do
		wait()
		if v.Parent:FindFirstChild('Humanoid') then
			if not table.find(playersOnHill, v.Parent.Name) then
				table.insert(playersOnHill, v.Parent.Name)
			end
		end
	end
	
	if #playersOnHill == 1 then
		if game.Players:FindFirstChild(playersOnHill[1]):FindFirstChild('TimeTaking') == nil and holder.Value ~= playersOnHill[1] then
			status.Value = "Contested"
			local stopWatch = Instance.new('IntValue')
			stopWatch.Name = "TimeTaking"
			stopWatch.Value = 0 
			stopWatch.Parent = game.Players:FindFirstChild(playersOnHill[1])
			counter(game.Players:FindFirstChild(playersOnHill[1]), stopWatch, playersOnHill)
		end
	end
	if #playersOnHill == 0 then
		for i,v in pairs(game:GetService('Players'):GetPlayers()) do
			wait()
			local intValue = v:FindFirstChild('TimeTaking')
			if intValue then
				intValue:Destroy()
			end
		end
	end
	for i,v in pairs(playersOnHill) do
		wait()
		table.remove(playersOnHill, 1)
	end
end

1 Like

Where is the script located? Is it a local script or a server script?

Try using the Changed event:

Example code:

local value = game.ReplicatedStorage.IDK

value.Changed:Connect(function()
–code–
end)

it is a server script in serverscriptservice

I will try that now, thanks for the suggestion.

Did it work or does it still not work?

I’m not sure how I can implement a changed event for a table

Just realized the problem, the script pauses until the counter() function is finished so the table never gets updated.