A Non-Player Instance trigger my script breaks it

As the title says, whenever a part that isn’t a Player touches my script, it completely breaks it and stops it from running. The script itself throws no errors in the console, it just stops running, and I have no idea why. I’d prefer to use the Players:FindFirstChild() method as a last-case scenario as I want my script to work in as many cases as possible. Here is my script:

TouchPart.Touched:Connect(function(Hit)
	print("Touched!")
	if Debounce == false then
		Debounce = true
		local Character = Hit.Parent
		local Player = Players:GetPlayerFromCharacter(Character)
		if Player then
			print("Is Player!")
			if GlobalDebounces == true then
				GiveItem(Player)
				task.wait(DebounceTime)
				Debounce = false
			else
				local Player = Players:GetPlayerFromCharacter(Character)
				local UserID = Player.UserId
				wait(0.01)
				if table.find(DebounceUsers, UserID) == nil then
					table.insert(DebounceUsers, UserID)
					GiveItem(Player)
					task.wait(0.05)
					Debounce = false
					task.wait(DebounceTime - 0.05)
					table.remove(DebounceUsers, table.find(DebounceUsers, UserID))
				else
					task.wait(0.05)
					Debounce = false
				end
			end
		end
	end
end)

PS: The Code is printing “Touched!” but not “Is Player!” so it must be breaking when it calls GetPlayerFromCharacter, but I have no idea how I’d do this without using that method, besides the method I stated up above.

1 Like

You’re only setting the debounce to false inside the if statement that checks if it’s a player. This means that when something touches the part that isn’t a player it will set the debounce to true and leave it like that, resulting in a lock.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.