Getting this error: attempt to index nil with 'UserId'

Hello!
So, I’m working on a bindable function which would return a player to the stage pad if they fail the obby.
My problem is debounce.
Bindable function is working fine if I remove “if checkDb == nil or checkDb == false then

However, I use the same debounce function in a similar script and it works fine there.

What should I do?
Script:

local part = script.Parent
local debounce = {}

local rS = game:GetService("ReplicatedStorage")
local rEvent = game.ReplicatedStorage.Remote.onFailTeleport

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local checkDb = debounce[player.UserId]
	if checkDb == nil or checkDb == false then
		debounce[player.UserId] = true
		rEvent:Fire(game.Players:GetPlayerFromCharacter(hit.Parent))
		wait(0.2)
		debounce[player.UserId] = false
	end
end)

You need to add a check to make sure the player was found, as hit could be anything from other parts, hats (parent is the hat not the player), etc.

local part = script.Parent
local debounce = {}

local rS = game:GetService("ReplicatedStorage")
local rEvent = game.ReplicatedStorage.Remote.onFailTeleport

part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local checkDb = debounce[player.UserId]
		if checkDb == nil or checkDb == false then
			debounce[player.UserId] = true
			rEvent:Fire(game.Players:GetPlayerFromCharacter(hit.Parent))
			wait(0.2)
			debounce[player.UserId] = false
		end
	end
end)
6 Likes

Thank you so much for the fast response! Works!