Why is killPlayer() not running?

The function killPlayer() isn’t running at all, even when I click the part and move. Everything else works just fine.

local debounced = {}
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if not table.find(debounced,plr.Name) then
		table.insert(debounced,plr.Name)
		game.ReplicatedStorage.memoryEvent:FireClient(plr)
		
		print(plr.Name)
		print(plr.Character:FindFirstChild('HumanoidRootPart').Position)
		local startingTick = tick()
		local rootpart = plr.Character:FindFirstChild('HumanoidRootPart')
		local hum = plr.Character:FindFirstChild('Humanoid')
		local connection
		
		local function killPlayer()
			print('deactivated')
			if tick() >= startingTick + 45 then
				print('ended')
				startingTick = nil
				rootpart = nil
				hum = nil
				connection:Disconnect()
				connection = nil
			else
				print('h')
			end
		end
		
		connection = rootpart:GetPropertyChangedSignal('Position'):Connect(killPlayer)
		task.wait(45)
		table.remove(debounced,table.find(debounced,plr.Name))
		startingTick = nil
		rootpart = nil
		hum = nil
		connection:Disconnect()
		connection = nil
		
	end
end)

I just checked the documentation for task.wait(). It says that the current thread is yielded until the time is up. Is it possible that task.wait() is preventing the GetPropertyChangedSignal from firing?

Edit: Sorry. Just looked it up and in your case it shouldn’t be the cause of the problem.

I think it still allows for connections to run. I removed it and it’s still not running.

1 Like

local connection
then theres nothing

Can you explain that more? I don’t understand what you mean.

What are you trying to achieve?

Make the killPlayer() function run when the player moves.

Try this instead:

local debounced = {}
script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	if not table.find(debounced,plr.Name) then
		table.insert(debounced,plr.Name)
		game.ReplicatedStorage.memoryEvent:FireClient(plr)
		
		print(plr.Name)
		print(plr.Character:FindFirstChild('HumanoidRootPart').Position)
		local startingTick = tick()
		local hum = plr.Character:FindFirstChild('Humanoid')
		local connection
		
		local function killPlayer()
			print('deactivated')
			if tick() >= startingTick + 45 and hum.MoveDirection.Magnitude > 0 then
				print('ended')
				startingTick = nil
				rootpart = nil
				hum = nil
				connection:Disconnect()
				connection = nil
			else
				print('h')
			end
		end
		
		connection = hum:GetPropertyChangedSignal('MoveDirection'):Connect(killPlayer)
		task.wait(45)
		table.remove(debounced,table.find(debounced,plr.Name))
		startingTick = nil
		rootpart = nil
		hum = nil
		connection:Disconnect()
		connection = nil
	end
end)

No, because it has to run when the player’s position changes at all.

I think there was some weird thing where the position of the local player wouldn’t appear to update.

You can try accessing the CFrame or something instead of the CFrame, but I can’t remember the exact work around